在spring上下文的配置文件中,加入需要管理的属性文件
<bean id="propertyConfigurer" class="com.sg.syj.base.webservice.ReadSpringProperty">
<property name="locations">
<list>
<value>WEB-INF/config/properties/jdbc.properties</value>
<value>WEB-INF/config/properties/webservice.properties</value>
</list>
</property>
</bean>
自定义类 ReadSpringProperty继承 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
package com.sg.syj.base;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/***
*
* @author czm
* 获取spring管理的属性文件
*
*/
public class ReadSpringProperty extends PropertyPlaceholderConfigurer {
private static Map<String, Object> propertiesMap;
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
propertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertiesMap.put(keyStr, value);
}
}
//根据属性文件的属性的属性名返回值
public static Object getContextProperty(String name) {
return propertiesMap.get(name);
}
}