使用两种单例模式加载系统参数

饿汉模式

package PropertyHoder;
    
    import java.io.IOException;
    import java.util.Properties;


public class Hungery {
	private  static  Properties  pro=new Properties();
	static  {
		try {
			pro.load(Hungery.class.getClassLoader().getResourceAsStream("collect.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public  static  Properties   getProps(){
		return  pro;
	}
	public static void main(String[] args) {
		
		Properties pro=Hungery.getProps();
		System.out.println(pro.getProperty("LOG_SOURCE_DIR"));
    }
}

懒汉模式

package PropertyHoder;

import java.io.IOException;
import java.util.Properties;


public class Lazy {
	private  static  Properties  pro=null;
	
	public  static Properties getPros() throws IOException{
		if(pro==null){
			synchronized(Lazy.class){
				if(pro==null){
				pro.load(Hungery.class.getClassLoader().getResourceAsStream("collect.properties"));
				}
			}
		}
		return  pro;
	}
public static void main(String[] args) {	
		Properties pro=Hungery.getProps();
		System.out.println(pro.getProperty("LOG_SOURCE_DIR"));
    }
}

系统参数

LOG_SOURCE_DIR=d:/logs/accesslog/
LOG_TOUPLOAD_DIR=d:/logs/toupload/
LOG_BACKUP_BASE_DIR=d:/logs/backup/
LOG_BACKUP_TIMEOUT=24
LOG_LEGAL_PREFIX=access.log.
HDFS_URI=hdfs://hdp-01:9000/
HDFS_DEST_BASE_DIR=/logs/
HDFS_FILE_PREFIX=access_log_
HDFS_FILE_SUFFIX=.log

文件目录
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_37719047/article/details/87969290