项目总结27:properties配置文件的读取(源码)
前言
使用properties文件配置的好处在于,可以统一管理一些可能会进行调整的参数;
直接上源码
配置文件:thirdpartyconfig.properties; 直接放在 resources目录下;
#这个配置类是为了配置实验室第三方对接的数据 lab_data_ws_url=http://XXX.XX.com:8000/Webjy.asmx/GetDataBYSyXX
配置读取类:ThirdPartyConfig;项目启动时,读取配置文件
package com.hs.api.service.thirdparty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.Properties; public class ThirdPartyConfig { private static final Logger log = LoggerFactory.getLogger(ThirdPartyConfig.class); private static Properties properties = new Properties(); private static final String SYSTEM_CONFIG_NAME = "thirdpartyconfig.properties"; private static ThirdPartyConfig singleton = new ThirdPartyConfig(); //在定义变量时就将其实例化 private ThirdPartyConfig(){ //System.out.println("构造函数被调用"); } public static ThirdPartyConfig getInstance(){ return singleton; } public static void load(){ String filePath = null; String confPath = System.getenv().get("CONFIG_PATH"); if(null == confPath){ load(SYSTEM_CONFIG_NAME); }else{ //System.out.println("进这里就读了环境变量--->"+confPath); if (!confPath.endsWith(File.separator)) { confPath = confPath + File.separator; } filePath = confPath + SYSTEM_CONFIG_NAME; File file = new File(filePath); if(file.exists()){ InputStream is = null; try { is = new FileInputStream(file); properties.load(is); }catch (FileNotFoundException e) { log.warn("The config file '" + filePath + "' does not exist.",e); }catch (IOException e) { log.warn("Failed to load the settings from the file: " + filePath); }finally { if (is != null) { try { is.close(); } catch (IOException e){} } } }else{ load(SYSTEM_CONFIG_NAME); } } } public static void load(String path){ //System.out.println("进这里就没做配置分离--->"+path); try { properties.load(ThirdPartyConfig.class.getResourceAsStream("/" + path)); } catch (FileNotFoundException e) { log.warn("The config file core-config.properties does not exist."); } catch (IOException e) { log.warn("Failed to load the settings from the file: core-config.properties"); } } static { load(); } public static String getConfig(String key) { return properties.getProperty(key); } public static void main(String args[]){ System.out.println(ThirdPartyConfig.getConfig("lab_data_ws_url")); } }
配置外放类:ThirdPartyConfigUtil;其他业务类可以通过ThirdPartyConfigUtil获取需要的参数
package com.hs.api.service.thirdparty; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ThirdPartyConfigUtil { private static final Log log = LogFactory.getLog(ThirdPartyConfigUtil.class); //实验数据 public String lab_data_ws_url = "";//webservice的URL private static final ThirdPartyConfigUtil single = new ThirdPartyConfigUtil(); //饿汉式单例类.在类初始化时,已经自行实例化 public static ThirdPartyConfigUtil getInstance() { return single; } private ThirdPartyConfigUtil() { initPropertis(); } private void initPropertis(){ lab_data_ws_url = ThirdPartyConfig.getConfig("lab_data_ws_url"); } }
测试类
package com.hs.api.service.thirdparty; /* * *@Description: *@Author:TYJ *@Date: create in 2019/5/28 11:24 */ public class test { public static void main(String[] args) { System.out.println(ThirdPartyConfigUtil.getInstance().lab_data_ws_url); //日志输出:http://XXX.XX.com:8000/Webjy.asmx/GetDataBYSyXX } }