转自;https://www.cnblogs.com/zrbfree/p/6230957.html

 import java.io.IOException;

 import java.io.InputStream;

 import java.util.NoSuchElementException;

 import java.util.Properties;

 import org.apache.commons.io.IOUtils;

 import org.slf4j.Logger;

 import org.slf4j.LoggerFactory;

 import org.springframework.core.io.DefaultResourceLoader;

 import org.springframework.core.io.Resource;

 import org.springframework.core.io.ResourceLoader;

 /**

  * Properties文件载入工具类. 可载入多个properties文件, *相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.

  */

 public class PropertiesLoader {

 private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);

 private static ResourceLoader resourceLoader = new DefaultResourceLoader();

 private final Properties properties;

 public PropertiesLoader(String... resourcesPaths) {

 properties = loadProperties(resourcesPaths);

 }

 public Properties getProperties() {

 return properties;

 }

 /**

  * 取出Property,但以System的Property优先,取不到返回空字符串.

  */

 private String getValue(String key) {

 String systemProperty = System.getProperty(key);

 if (systemProperty != null) {

 return systemProperty;

 }

 if (properties.containsKey(key)) {

         return properties.getProperty(key);

     }

     return "";

 }

 /**

  * 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.

  */

 public String getProperty(String key) {

 String value = getValue(key);

 if (value == null) {

 throw new NoSuchElementException();

 }

 return value;

 }

 /**

  * 取出String类型的Property,但以System的Property优先.如果都为Null则返回Default值.

  */

 public String getProperty(String key, String defaultValue) {

 String value = getValue(key);

 return value != null ? value : defaultValue;

 }

 /**

  * 取出Integer类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.

  */

 public Integer getInteger(String key) {

 String value = getValue(key);

 if (value == null) {

 throw new NoSuchElementException();

 }

 return Integer.valueOf(value);

 }

 /**

  * 取出Integer类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常

  */

 public Integer getInteger(String key, Integer defaultValue) {

 String value = getValue(key);

 return value != null ? Integer.valueOf(value) : defaultValue;

 }

 /**

  * 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.

  */

 public Double getDouble(String key) {

 String value = getValue(key);

 if (value == null) {

 throw new NoSuchElementException();

 }

 return Double.valueOf(value);

 }

 /**

  * 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常

  */

 public Double getDouble(String key, Integer defaultValue) {

 String value = getValue(key);

 return value != null ? Double.valueOf(value) : defaultValue;

 }

 /**

  * 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.

  */

 public Boolean getBoolean(String key) {

 String value = getValue(key);

 if (value == null) {

 throw new NoSuchElementException();

 }

 return Boolean.valueOf(value);

 }

 /**

  * 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.

  */

 public Boolean getBoolean(String key, boolean defaultValue) {

 String value = getValue(key);

 return value != null ? Boolean.valueOf(value) : defaultValue;

 }

 /**

  * 载入多个文件, 文件路径使用Spring Resource格式.

  */

 private Properties loadProperties(String... resourcesPaths) {

 Properties props = new Properties();

 for (String location : resourcesPaths) {

 logger.debug("Loading properties file from:" + location);

 InputStream is = null;

 try {

 Resource resource = resourceLoader.getResource(location);

 is = resource.getInputStream();

 props.load(is);

 } catch (IOException ex) {

 logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());

 } finally {

 IOUtils.closeQuietly(is);

 }

 }

 return props;

 }

 }

使用spring的DefaultResourceLoader自定义properties文件加载工具类的更多相关文章

  1. 161216、使用spring的DefaultResourceLoader自定义properties文件加载工具类

    import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; imp ...

  2. log4j 日志脱敏处理 + java properties文件加载

    Java 加载Properties 配置文件: ResourceBundle bundle = ResourceBundle.getBundle("log4j_filter"); ...

  3. Android 菊花加载工具类

    先看看实现效果图 1.首先自定义一个类继承系统ProgressDialog /** * Created by hanbao0928 on 2018/11/1. */ public class Dial ...

  4. JAVA中自定义properties文件介绍

    Gradle中的使用 1. 使用gradle.properties buid.gradle 和 gradle.properties可以项目使用,在同一个项目中,build.gradle可以直接获取其同 ...

  5. jdbc.properties不能加载到tomcat项目下面

    javaweb项目的一个坑,每次重启tomcat都不能将项目中的jdbc.properties文件加载到tomcat项目对应的classes目录下面,得手动粘贴到该目录下.

  6. android实现异步加载图片类

    其中牵涉到的关键知识点 1,回调机制,不过回调接口的实现方式有多种多样,可以是一个类继承该接口,也可以是作为一个方法参数: 可以参照自己的这篇博客: http://www.cnblogs.com/bo ...

  7. spring入门(二)【加载properties文件】

    在开发过程当中需要用到配置信息,这些信息不能进行硬编码,这时配置文件是一个比较好的方式,java提供了properties格式的文件,以键值对的方式保存信息,在读取的时候通过键获得键对应的值,spri ...

  8. Spring Boot自定义配置与加载

    Spring Boot自定义配置与加载 application.properties主要用来配置数据库连接.日志相关配置等.除了这些配置内容之外,还可以自定义一些配置项,如: my.config.ms ...

  9. Spring项目中Properties不能加载多个的问题

    A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件: A模块 A模块的Spring配置文件如下: <?xml version="1.0" enc ...

随机推荐

  1. (十一)python3 encode()和decode()

    从英文意思上看,encode和decode分别指编码和解码.在python中,Unicode类型是作为编码的基础类型,即: decode encode str ---------> str(Un ...

  2. Swagger UI教程

    文档源地址 http://www.68idc.cn/help/makewebs/qitaasks/20160621620667.html Swagger-UI本身只提供在线测试功能,要集成它还需要告诉 ...

  3. C语言基础--自加自减

    有如下代码: unsigned int temp1,temp2, i=5,j=5; temp1=i++; temp2=++j; 结果是 temp1=5,temp2=6: i=6,j=6: 版权声明:本 ...

  4. Xcode报referenced from错误的总结

    一.库文件丢失 如果提示的文件是库文件,比如说是sdk的文件,有可能是就是丢失,或者没有引用到该工程. 1.点击这个.a库,或者framework,看右边的target里面是否引用到了当前的targe ...

  5. JDK的安装和环境变量配置

    1.安装JDK开发环境 下载网站: http://www.oracle.com/technetwork/java/javase/downloads/index.html 进入后选择Accept Lic ...

  6. 【转】Java读写文件大全

    使用Java操作文本文件的方法详解        最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(ch ...

  7. [luoguP2401] 不等数列

    传送门 f[i][j]表示前i个数有j个<的方案数 #include <cstdio> #define N 1001 #define p 2015 int n, k; int f[N ...

  8. 权限管理组件:rbac

    rbac: Role_Based Access Control,基于角色的权限控制 权限:一个包含正则表达式 的url就是一个权限 目录结构: rbac这个app中的文件代码如下: rbac/mode ...

  9. 【BZOJ2301】Problem b(莫比乌斯反演)

    题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d, 且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 1≤n≤50000,1≤a≤b≤50000 ...

  10. HDU 4416 (后缀自动机)

    HDU 4416 Good Article Good sentence Problem : 给一个串S,和一些串T,询问S中有多少个子串没有在T中出现. Solution :首先对所有的T串建立后缀自 ...