转自;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. Hadoop2.x异常总结

    问题1: 在执行bin/hdfs namenode -format格式化HDFS命令时,抛出异常,异常如下: 16/10/26 18:32:45 ERROR namenode.NameNode: Fa ...

  2. 第一节:python提取PDF文档中的图片

    由于项目需要将PDF文档当中的图片转换成图片,所以参考了这篇文章https://blog.csdn.net/qq_15969343/article/details/81673302后项目得以解决. 1 ...

  3. 杭电 2037 今年暑假不AC

    Problem Description “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACM ...

  4. 【Codeforces 1031C】Cram Time

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 如果找到最大的n使得1+2+...+n<=a+b 然后第一天输出1,2.3,4....t1 这里1+2+..+t1<=a 这还远远 ...

  5. js变量类型详解

    <html> <title>js变量类型详解</title> <meta http-equiv="content-type" conten ...

  6. HTML5 & CSS3 & font-family

    HTML5 & CSS3 & font-family 中文字体的英文名称 宋体* SimSun 黑体* SimHei 微软雅黑* Microsoft YaHei 微软正黑体 http: ...

  7. parse XML & js

    parse XML & js how to parse xml data in js? https://stackoverflow.com/questions/17604071/parse-x ...

  8. 【Tomcat】Tomcat Connector的三种运行模式【bio、nio、apr】

    Tomcat Connector(Tomcat连接器)有bio.nio.apr三种运行模式 bio bio(blocking I/O,阻塞式I/O操作),表示Tomcat使用的是传统的Java I/O ...

  9. ****使用ftp软件上传下载php文件时换行符丢失bug

    在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下notepad++编辑器写好的php文件,在使用ftp上传到linux服务器后,php文件的换行符全部丢失了,导致php文件 ...

  10. CSS类选择器

    CSS 选择器参考手册 还是   .class     #id    element  用的最多! 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. "CSS" 列指 ...