转自;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. [WPF自定义控件库]为Form和自定义Window添加FunctionBar

    1. 前言 我常常看到同一个应用程序中的表单的按钮----也就是"确定"."取消"那两个按钮----实现得千奇百怪,其实只要使用统一的Style起码就可以统一按 ...

  2. 全文检索(AB-1)-相关领域

    信息检索 认知科学 管理科学

  3. PowerDesigner物理模型用法总结

    1.  生成sql脚本 Database→Generate Database 选择要输出的文件路径,即文件存储路径,并根据需要修改文件名,单击确定后便会生成sql脚本. 在Options选项卡里,可以 ...

  4. poj——1469 COURSES

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24192   Accepted: 9426 Descript ...

  5. 洛谷——P1151 子数整数

    P1151 子数整数 题目描述 对于一个五位数a1a2a3a4a5,可将其拆分为三个子数: sub1=a1a2a3 sub2=a2a3a4 sub3=a3a4a5 例如,五位数20207可以拆分成 s ...

  6. springboot + mybatis 完成图片上传并保存到数据库

    添加依赖 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons- ...

  7. Spring Cloud体系实现标签路由

    如果你正在使用Spring Cloud体系,在实际使用过程中正遇到以下问题,可以阅读本文章的内容作为后续你解决这些问题的参考,文章内容不保证无错,请务必仔细思考之后再进行实践. 问题: 1,本地连上开 ...

  8. component and slot

    component and slot 使用: 1.component panel <article class="message"> <div class=&qu ...

  9. 基于Wi-Fi的HID注射器,利用WHID攻击实验

    WHID代表基于 Wi-Fi 的 HID 注射器,即对 HID 攻击进行无线化攻击的一种注入工具. 实验攻击原理如下图: 攻击者使用ESP8266作为AP,在自己的电脑创建客户端连接AP.在客户端键入 ...

  10. DHCP Snooping的实现

    DHCP Snooping的实现 DHCP Snooping的实现 主要作用:1.防止在动态获得IP地址的网络环境中用户手动配置PC的IP地址;2.防止A用户的PC静态配置的IP地址顶掉B用户PC动态 ...