先准备測试程序:
package org.jediael.util; import static org.junit.Assert.*; import org.junit.Test; public class BasicConfigurationTest { @Test public void testGetValue(){ BasicConfiguration configuration = BasicConfiguration.getInstance(); assertTrue(configuration.getValue("key").equals("value")); } }
当中properties文件里有一行例如以下:
key=value
优先选择方案三
方式一:懒汉方式
到第一次使用实例时。才载入实例
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(); } } } }上述程序中。产生了BasicConfiguration的一个单例。
优点是仅仅有到第一次调用getInstance才生成对象,节省了空间。不足之处在于同步锁导致有可能运行过慢。
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的实例是static。因此,当类被载入时就会初始化,但这样即使并不须要使用此实例,也会被初始化,导致内存空间的浪费。
方式三:
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(); } } } }
因为初始化放在内部类中,仅仅有当此内部类被使用时,才会进行初始化。
从而既节省了空间。也无需同步代码。