PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。
属性文件
以为例:
配置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 : ()) {
String keyStr = ();
String value = (keyStr);
(keyStr, value);
}
}
public static Object getContextProperty(String name) {
return (name);
}
public static Object setContextProperty(String name,Object value) {
return (name, value);
}
public static Map<String, Object> getContextPropertyMap() {
return ctxPropertiesMap;
}
}
配置
往spring容器中注入bean
<bean id="appConfigUtil" class="">
<property name="locations">
<list>
<value>classpath*:</value>
</list>
</property>
</bean>
使用
AppConfigUtil.getContextProperty("filePath");
其他相关
关于PropertyPlaceholderConfigurer的使用可以参看:
/blog/435165
/yl2755/archive/2012/05/06/
spring获取bean:
/yangzhilong/p/
java读取properties文件:
/hafiz/p/