使用单例模式加载properties文件
生活随笔
收集整理的這篇文章主要介紹了
使用单例模式加载properties文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用單例模式加載properties文件
@(JAVA)[java]
* 這里只是用于示范單例模式,更好的辦法是使用java.util.ResourceBundle解釋proerties文件。詳見java.util.ResourceBundle的介紹文章 *
先準(zhǔn)備測試程序
package org.jediael.util; import static org.junit.Assert.*; import org.junit.Test;public class BasicConfigurationTest {@Testpublic void testGetValue(){BasicConfiguration configuration = BasicConfiguration.getInstance();assertTrue(configuration.getValue("key").equals("value"));} }其中properties文件中有一行如下:
key=value優(yōu)先選擇方案三
方式一:懶漢方式
到第一次使用實(shí)例時(shí),才加載實(shí)例
package org.jediael.util;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties;public class BasicConfiguration {private static BasicConfiguration configuration = null;private Properties pros = null;public static synchronized BasicConfiguration getInstance(){if(configuration == null){configuration = new BasicConfiguration();}return configuration;}public String getValue(String key){return pros.getProperty(key);}private BasicConfiguration(){readConfig();}private void readConfig() {pros = new Properties();InputStream in = null;try {in = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("").getPath() + "search.properties");pros.load(in);} catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally{try {in.close();} catch (IOException e) {e.printStackTrace();}}} }上述程序中,產(chǎn)生了BasicConfiguration的一個單例。
好處是只有到第一次調(diào)用getInstance才生成對象,節(jié)省了空間。不足之處在于同步鎖導(dǎo)致有可能執(zhí)行過慢。
2、餓漢方式
package org.jediael.util;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties;public class BasicConfiguration {private static BasicConfiguration configuration = new BasicConfiguration();private Properties pros = null;public static BasicConfiguration getInstance(){return configuration;}public String getValue(String key){return pros.getProperty(key);}private BasicConfiguration(){readConfig();}private void readConfig() {pros = new Properties();InputStream in = null;try {in = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("").getPath() + "search.properties");pros.load(in);} catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally{try {in.close();} catch (IOException e) {e.printStackTrace();}}} }由于BasicConfiguration的實(shí)例是static,因此,當(dāng)類被加載時(shí)就會初始化,但這樣即使并不需要使用此實(shí)例,也會被初始化,導(dǎo)致內(nèi)存空間的浪費(fèi)。
方式三:內(nèi)部類方式
由于初始化放在內(nèi)部類中,只有當(dāng)此內(nèi)部類被使用時(shí),才會進(jìn)行初始化。從而既節(jié)省了空間,也無需同步代碼。
package org.jediael.util;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties;public class BasicConfiguration {private Properties pros = null;private static class ConfigurationHolder{private static BasicConfiguration configuration = new BasicConfiguration();}public static BasicConfiguration getInstance(){return ConfigurationHolder.configuration;}public String getValue(String key){return pros.getProperty(key);}private BasicConfiguration(){readConfig();}private void readConfig() {pros = new Properties();InputStream in = null;try {in = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("").getPath() + "search.properties");pros.load(in);} catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally{try {in.close();} catch (IOException e) {e.printStackTrace();}}} }總結(jié)
以上是生活随笔為你收集整理的使用单例模式加载properties文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java路径问题最终解决方案—可定位所有
- 下一篇: String, StringBuilde