一、自定义初始化的类继承org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
重写processProperties(...)方法把读取的properties依次读取放到系统的缓存中。对外提供getProperty(String name)方法作为java代码中需要读取property时调用的方法.
代码如下:
public class CustomizedPropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
private static Map<String, Object> propertiesMap = null;
@Override
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 = ObjectUtils.toString(key);
propertiesMap.put(keyStr, props.getProperty(keyStr));
}
}
public static Object getPorperty(String name){
return propertiesMap.get(name);
}
}
二、在spring配置文件中将参数初始化类配置为上面自定义的类
<bean id="propertyConfigurer"
class="com.qiyuntian.store.base.utils.CustomizedPropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:env.properties</value>
</property>
</bean>
这样就可以再java代码中读取,properties文件中的内容了。