JAVA获取内部及外部配置
2016-04-29 11:05
459 查看
获取内部配置
PropertiesUtils.java
获取外部配置
ExternalPropertiesUtils.java
PropertiesUtils.java
package cn.ting.common.util; import java.io.IOException; import java.util.Properties; import org.apache.log4j.Logger; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; public class PropertiesUtils { private static final Logger log = Logger.getLogger("PropertiesUtils"); private static PropertiesUtils propertiesUtils = null; private Properties config = null; public PropertiesUtils(){ try { Resource resource = new ClassPathResource("/env.properties"); config = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException e) { log.error(e.getMessage()); throw new RuntimeException("找不到配置文件:env.properties"); } } public static String getProperty(String key){ return getConfig().getProperty(key); } private static Properties getConfig(){ if(null == propertiesUtils){ propertiesUtils = new PropertiesUtils(); } return propertiesUtils.config; } }
获取外部配置
ExternalPropertiesUtils.java
package cn.ting.common.util; import java.io.IOException; import java.util.Properties; import org.apache.log4j.Logger; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; public class ExternalPropertiesUtils { private static final Logger log = Logger.getLogger("ExternalPropertiesUtils"); private static ExternalPropertiesUtils externalPropertiesUtils = null; private Properties config = null; public ExternalPropertiesUtils(){ try { String path = PropertiesUtils.getProperty("external.properties.path"); Resource resource = new FileSystemResource(path); config = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException e) { log.error(e.getMessage()); throw new RuntimeException("找不到外部配置文件"); } } public static String getProperty(String key){ return getConfig().getProperty(key); } private static Properties getConfig(){ if(null == externalPropertiesUtils){ externalPropertiesUtils = new ExternalPropertiesUtils(); } return externalPropertiesUtils.config; } }
相关文章推荐
- Java中Double类型的四舍五入
- myeclipse注释模板修改
- Eclipse安装Maven插件
- eclipse出现真机不能安装程序
- hduoj4762(Java处理大数)
- Java学习之旅基础知识篇:数组及引用类型内存分配
- JAVA 常用设计模式
- spring-data-jpa 中文文档(1)
- java类中定义接口的有哪些好处
- JAVA中的关键字
- 杂记:java 并发 的东东
- java的编码原理
- Spring4.0.6 Websocket详细配置 之 讯息模块
- 73、JAVA中int、String的类型转换
- java ThreadPoolExecutor
- Webservice接口开发,Spring注入失败
- Java Comparator 和 Strategy模式
- java创建线程的四种方式
- javadoc生成出现错误“编码 GBK 的不可映射字符
- Java 可执行Jar包打包方法