Spring PropertyPlaceholderConfigurer获取属性文件值
来源:互联网 发布:布尔人 知乎 编辑:程序博客网 时间:2024/06/05 03:45
PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。
属性文件
以app.properties为例:
配置key-value:
filePath = /Users/loukai/easylife/files/
自定义属性加载工具类
该类必须要继承 PropertyPlaceholderConfigurer类(实际是对PropertyPlaceholderConfigurer类的一个扩展)
public class AppConfigUtil extends PropertyPlaceholderConfigurer{ private static Map<String, Object> ctxPropertiesMap; /** * 重写processProperties方法 */ @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); ctxPropertiesMap = new HashMap<String, Object>(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } public static Object getContextProperty(String name) { return ctxPropertiesMap.get(name); } public static Object setContextProperty(String name,Object value) { return ctxPropertiesMap.put(name, value); } public static Map<String, Object> getContextPropertyMap() { return ctxPropertiesMap; }}
applicationContext.xml配置
往spring容器中注入bean
<bean id="appConfigUtil" class="com.easylife.core.util.AppConfigUtil"> <property name="locations"> <list> <value>classpath*:app.properties</value> </list> </property> </bean>
使用
AppConfigUtil.getContextProperty("filePath");
其他相关
关于PropertyPlaceholderConfigurer的使用可以参看:
http://seraph115.iteye.com/blog/435165
http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html
spring获取bean:
http://www.cnblogs.com/yangzhilong/p/3949332.html
java读取properties文件:
http://www.cnblogs.com/hafiz/p/5876243.html
阅读全文
0 0
- Spring PropertyPlaceholderConfigurer获取属性文件值
- PropertyPlaceholderConfigurer在spring获取属性文件
- PropertyPlaceholderConfigurer获取属性文件指定key的value值
- Spring PropertyPlaceholderConfigurer解析Java Properties属性文件值
- spring里PropertyPlaceholderConfigurer读取属性文件
- spring PropertyPlaceholderConfigurer自定义属性
- 编码方式获取Spring中PropertyPlaceholderConfigurer的属性
- 使用PropertyPlaceholderConfigurer读取.properties文件(3)-公共类获取属性
- spring中使用外部属性文件(关于PropertyPlaceholderConfigurer)
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- 使用spring的PropertyPlaceholderConfigurer加密properties文件中的属性
- Spring中扩展 PropertyPlaceholderConfigurer处理加密属性文件
- Spring属性占位符PropertyPlaceholderConfigurer
- spring 配置文件 获取变量(PropertyPlaceholderConfigurer)
- Spring属性占位符PropertyPlaceholderConfigurer的使用
- Spring属性占位符PropertyPlaceholderConfigurer的使用
- HDU
- 算法设计与应用基础
- do...while(0)解析
- Solr基础入门介绍
- 微信小程序学习笔记(1)
- Spring PropertyPlaceholderConfigurer获取属性文件值
- C语言文件操作lseek
- Linux后台Daemon中打印debug message的方法
- C语言指针的例题
- C++ 06 —— 拷贝构造函数
- 关于CSS的总结
- 12. Integer to Roman
- java 枚举
- 算法设计与应用基础