转自;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. jsp学习之如何在web层创建Servlet

    jsp动态网页的文件目录结构如下: 1.src下存放java代码 2.包web_xx为web层 3.webcontent里面是jsp文件 jsp页面中的请求交付给servlet处理,在实际中 jsp的 ...

  2. [luoguP2146] 软件包管理器(树链剖分)

    传送门 看着很吓人,其实就是个树链剖分模板. 可支持操作: 1.将节点 x 到 根 的路径上的值都变成 1 2.将以节点 x 为根的子树的值都变成 0 1A爽~ ——代码 #include <c ...

  3. 新vim配置文件

    "******************************************************特殊设置************************************ ...

  4. UVA 437_The Tower of Babylon

    题意: 一堆石头,给定长宽高,每种石头均可以使用无数次,问这堆石头可以叠放的最高高度,要求下面的石头的长和宽分别严格大于上面石头的长和宽. 分析: 采用DAG最长路算法,由于长宽较大,不能直接用于表示 ...

  5. 使用idea编译spring-framework5.0源码

    自从迈入java开发这个行当,从来没有好好的研究过源码,深感惭愧,话不多说,今天上一篇使用idea编译spring5.0源码. 以下在win中构建和编译过程分为 jdk环境的配置 gradle的下载和 ...

  6. 安装adt-bundle-windows-x86-20130917时遇到的问题及解决方法

    最近在上安卓课,老师让我们下载此软件(adt-bundle-windows-x86-20130917.下载压缩后,打开eclipse的时候,会出现以下情况: 这时说明你的jdk还没下载或者下载错位置了 ...

  7. operamasks—omBorderLayout布局

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. Ubuntu 16.04中XMind 8导致Java内存溢出的问题解决(硬盘卡死,桌面卡死)

    XMind使用的是Java进行开发,如果出现内存溢出的问题,那么一定是桌面快捷方式的问题,解决方法是直接修改快捷方式里面的内容,修改如下: [Desktop Entry] Encoding=UTF-8 ...

  9. paramiko错误信息:Paramiko error: size mismatch in put

    在使用paramiko的put往远处服务器上传资源的时候,出现类似下面的错误信息 The code in paramiko's sftp_client.py:putfo() reads at the ...

  10. jupyter-notebook添加python虚拟环境的kernel

    参考: jupyter notebook添加kernel 在jupyter notebook上使用虚拟环境 本文是在anaconda的环境下配置的,装好anaconda后,jupyter-notebo ...