Java读取properties文件(非泛滥)
根据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文件(非泛滥)的更多相关文章
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- 用java读取properties文件--转
今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享. 下面直接贴出代码:java类 public class Mytest pub ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java基础学习总结——java读取properties文件总结
摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...
- java读取properties文件时候要注意的地方
java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...
- java基础—java读取properties文件
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- Java基础学习总结(15)——java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- 【开发笔记】- Java读取properties文件的五种方式
原文地址:https://www.cnblogs.com/hafiz/p/5876243.html 一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供j ...
随机推荐
- [leetcode-611-Valid Triangle Number]
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- Thrift总结(二)创建RPC服务
前面介绍了thrift 基础的东西,怎么写thrift 语法规范编写脚本,如何生成相关的语言的接口.不清楚的可以看这个<Thrift总结(一)介绍>.做好之前的准备工作以后,下面就开始如何 ...
- KVO底层实现原理,仿写KVO
这篇文章简单介绍苹果的KVO底层是怎么实现的,自己仿照KVO的底层实现,写一个自己的KVO监听 #pragma mark--KVO底层实现 第一步:新建一个Person类继承NSObject Pers ...
- 分页插件Jpages的使用
项目原因需要前端做分页表格,之前做了一个ul的分页效果,但是感觉自己写还是造轮子了,今天网上看到Jpqges插件就试了下,感觉平时使用挺方便的,写一下自己的使用过程. 先上套图,下载下来就2个js和1 ...
- android权限(permission)大全
权限添加位置: 权限代码: 1.android.permission.WRITE_USER_DICTIONARY允许应用程序向用户词典中写入新词 2.android.permission.WRITE_ ...
- accp8.0转换教材第7章JavaScript操作DOM对象理解与练习
程序调试,chrome开发人员工具,DOM操作,节点和节点间的关系,节点信息,操作节点,获取元素 一.单词部分 ①alert警告②prompt提示③parentNode父节点④childNode子节点 ...
- windows 10 下使用 binwalk
刚接触CTF没什么经验,菜鸟一只很多题不会做,就在网上看大佬写的Write up.发现经常会用到一个小工具--binwalk.binwalk在kali系统里是一个自带的工具,但windows可没有.之 ...
- C++强制类型转换:static_cast、dynamic_cast、const_cast、reinterpret_cast
1. c强制转换与c++强制转换 c语言强制类型转换主要用于基础的数据类型间的转换,语法为: (type-id)expression//转换格式1 type-id(expression)//转换格式2 ...
- 为何你跟着滴滴D8级前端大神撸代码,技术却依旧原地踏步?
引子 听说最近有很多小伙伴,热衷于在慕课网上学习各种前端实战教程,并以完成项目为奋斗目标.比如本文接下来要提到的<Vue2.0高级实战之开发移动端音乐App>,这门课程的传授者是来自滴滴D ...
- SQL Server2008 安装及概述
最近在学习SQL Server 数据库,用的版本是2008 R2的版本,如下图所示,本人学习时间不长,写这篇文章一是为了锻炼自己加深印象,二也可以与和诸多朋友进行交流以及得到大家的指教. 安装教程网上 ...