zoukankan      html  css  js  c++  java
  • 【设计模式:单例模式】使用单例模式载入properties文件


    先准备測试程序:

    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();
    			}
    		}
    	}
    }

    因为初始化放在内部类中,仅仅有当此内部类被使用时,才会进行初始化。

    从而既节省了空间。也无需同步代码。





  • 相关阅读:
    简单分析实现运维利器---web远程ssh终端录像回放libl
    利用kite对视频流应用进行压力测试
    Springboot 启动扩展
    SpringBoot 自动配置原理
    idea springboot没有启动项,或启动时找不到或无法加载主类
    Elasticsearch、Kibana、elasticsearch-analysis-ik 版本下载地址
    Springboot 操作Elasticsearch 方式二 【rest-high-level-client】
    Elasticsearch 安装x-pack之后,无法连接head问题
    ES版本是向下兼容的,springboot连接ES,可以用低版本客户端
    ES安装elasticsearch-head-master插件
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5230529.html
Copyright © 2011-2022 走看看