根据spring配置文件的 PropertiesFactoryBean和 PropertyPlaceholderConfigurer可以选择不同的加载方式,我是使用System.setProperty(key, value),代码中可以直接用System.getProperty(key)取value的值

一、PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer是解决 properties 文件占位符问题,实现 PropertiesLoaderSupport 类

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
<!-- 忽略不可解析的 -->
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
package com.phil.common.prop;

import java.util.Map.Entry;
import java.util.Properties; import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component; /**
* 加载Properties文件
* @author phil
* @date 2017年8月13日
*
*/
@Component
public class InitializingProperties implements InitializingBean { private static final Logger logger = Logger.getLogger(InitializingProperties.class); /*
* (non-Javadoc)
*
* @see
* org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
Properties props = new Properties();
props.load(InitializingProperties.class.getClassLoader().getResourceAsStream("test1.properties"));
props.load(InitializingProperties.class.getClassLoader().getResourceAsStream("test2.properties"));
for (Entry<Object, Object> e : props.entrySet()) {
System.setProperty(e.getKey().toString(), e.getValue().toString());
logger.info(e.getKey().toString() + "---" + e.getValue().toString());
}
}
}

二、PropertiesFactoryBean

PropertiesFactoryBean 是PropertiesLoaderSupport 直接的实现类

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>classpath:portal_dev.properties</value>
</property>
</bean>
package com.phil.common.prop;

import java.util.Map.Entry;
import java.util.Properties; import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; /**
* 加载Properties文件
* @author phil
* @date 2017年8月13日
*
*/
@Component
public class InitializingProperties implements InitializingBean { private static final Logger logger = Logger.getLogger(InitializingProperties.class); @Autowired
private Properties propertyConfigurer; /*
* (non-Javadoc)
*
* @see
* org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
for (Entry<Object, Object> e : propertyConfigurer.entrySet()) {
System.setProperty(e.getKey().toString(), e.getValue().toString());
logger.info(e.getKey().toString() + "---" + e.getValue().toString());
}
}
}

Java读取properties文件(非泛滥)的更多相关文章

  1. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  2. 用java读取properties文件--转

    今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest pub ...

  3. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  4. java基础学习总结——java读取properties文件总结

    摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...

  5. java读取properties文件时候要注意的地方

    java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...

  6. java基础—java读取properties文件

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  7. Java基础学习总结(15)——java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  8. java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  9. 【开发笔记】- Java读取properties文件的五种方式

    原文地址:https://www.cnblogs.com/hafiz/p/5876243.html 一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供j ...

随机推荐

  1. Vim按Esc后光标左移问题的解决

    参考了这篇文章http://vim.wikia.com/wiki/Prevent_escape_from_moving_the_cursor_one_character_to_the_left 在Vi ...

  2. 严重: Could not synchronize database state with session org.hibernate.exception.DataException: Could not execute JDBC batch update

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #ff2600 } p.p2 { margin: 0.0px 0 ...

  3. servlet+jsp导入Excel到mysql数据库

    package khservlet; import java.io.FileInputStream;import java.io.IOException;import java.io.InputStr ...

  4. webpack初步介绍

    我们通过npm -g可以安装一个webpack的东西. npm -g叫做全局安装,通常是安装CLI程序(commond line interface). 我们只用过一次,装了cnpm.此时就能在CMD ...

  5. windows 10 下使用 binwalk

    刚接触CTF没什么经验,菜鸟一只很多题不会做,就在网上看大佬写的Write up.发现经常会用到一个小工具--binwalk.binwalk在kali系统里是一个自带的工具,但windows可没有.之 ...

  6. 拥抱.NET Core系列:依赖注入(2)

    上一篇"拥抱.NET Core系列:依赖注入(1)"大体介绍了服务注册.获取和生命周期,这一篇来做一些补充. 由于内容跨度大(.NET Core.ASP.NET Core),所以文 ...

  7. Centos7架设NMP服务器笔记

    安装centos7.3 1.从mirrors.163.com下载7.3 2.准备虚拟机vitualbox,网络我使用的桥接到无线网卡,直接连我到路由器,IP自动分配(本来想搞静态IP的,搞了好久没成功 ...

  8. JS内置对象学习总结

    日期对象: 创建日期对象: var date=new Date();//创建日期对象 设置/返回年份方法: date.getFullYear(); date.setFullYear(); 返回星期的方 ...

  9. Java List Remove时要注意的细节

    1.如果你是在遍历的时候去remove一个对象 for(int i = 0, length = list.size(); i<length; i++){} 这种遍历需要每次remove时,对i- ...

  10. php与MySQL(基本操作)

    PHP连接 MySQL 在我们访问 MySQL 数据库前,我们需要先连接到数据库服务器,连接服务器,我们使用mysqli_connect()函数. 在使用这个函数之前,我们首先来看一下这个函数的语法: ...