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;

}

}

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

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

    转自:https://www.cnblogs.com/zrbfree/p/6230957.html import java.io.IOException; import java.io.InputSt ...

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

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

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

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

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

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

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

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

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

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

  7. spring boot 文件上传工具类(bug 已修改)

    以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...

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

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

  9. spring 属性文件加载接口---PropertySourceLoader

    org.springframework.boot.config Interface PropertySourceLoader 实现类: PropertiesPropertySourceLoader, ...

随机推荐

  1. CSS的class、id、css文件名的常用命名规则

    CSS的class.id.css文件名的常用命名规则        (一)常用的CSS命名规则 头:header       内容:content/container       尾:footer   ...

  2. PCL Show Point Cloud 显示点云

    在使用PCL库的时候,经常需要显示点云,可以用下面这段代码: #include <pcl/visualization/cloud_viewer.h> pcl::PointCloud< ...

  3. Effective STL(第7条)

    第7条:如果容器中包含了通过new操作创建的指针,切忌在容器对象析构前将指针delete掉 //向一个vector中添加多个new出来的对象 void doSomething(){ vector< ...

  4. apache中.htaccess不起作用

    找到apache的配置文件httpd.conf文件,找到:  代码如下 复制代码 #LoadModule rewrite_module modules/mod_rewrite.so 去掉前面的#号. ...

  5. HTML静态网页 css样式表

    CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/    此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控 ...

  6. Flashback闪回技术小实验

    1闪回查询 2闪回删除 3闪回版本查询 4闪回事务查询 5闪回某张表   6闪回数据库

  7. 最轻量级的前端Mvc框架backbone

    最轻量级的前端Mvc框架backbone依赖最轻量级的库understore backbone并非将前端再次切分为mvc,而是分为了七大模块,分别是:Events.Model.Collection.R ...

  8. 20145334赵文豪 《Java程序设计》第4周学习总结

    20145334赵文豪 <Java程序设计>第4周学习总结 教材学习内容总结 第六章知识点总结 1-继承共同行为:如果在程序设计上存在着重复,那就需要修改,可以吧相同的程序代码提升(pul ...

  9. demo工程的清单文件及activity中api代码简单示例

    第一步注册一个账户,并创建一个应用.获取app ID与 app Key. 第二步下载sdk 第三步新建工程,修改清单文件,导入相关的sdk文件及调用相应的api搞定. 3.1 修改清单文件,主要是加入 ...

  10. BizTalk动手实验(一)安装BizTalk Server 2010开发环境

    1 课程简介 通过本课程了解BizTalk 2010的软依赖及基本的安装配置步骤,BizTalk相应的解决方案及高可用性方案可在课程的基础进行深入学习. 2 准备工作 硬件环境:CPU >2.0 ...