package com.liyang.utils;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class PropertiesUtil extends PropertyPlaceholderConfigurer {
private static Map<String, String> map = new HashMap<String, String>() ;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) {
super.processProperties(beanFactory, props) ;
for (Object key : props.keySet()) {
String keyStr = key.toString() ;
String value = props.getProperty(keyStr) ;
map.put(keyStr, value) ;
}
}
public static String getProperty(String key) {
return map.get(key);
}
}
<bean id="propertyConfigurer" class="com.liyang.utils.PropertiesUtil">
<property name="locations">
<list>
<value> classpath:app.properties </value>
<value> classpath:app2.properties </value>
</list>
</property>
</bean>
package com.liyang.utils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class EncryptPropertiesUtil extends PropertyPlaceholderConfigurer {
private static Map<String, String> map = new HashMap<String, String>() ;
private static List<String> toEncrypt = Arrays.asList("user" , "password") ;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props){
super.processProperties(beanFactory, props) ;
for (Object key : props.keySet()) {
String keyStr = key.toString() ;
String value = props.getProperty(keyStr) ;
map.put(keyStr, value) ;
}
}
@Override
protected String convertProperty(String propertyName, String propertyValue){
return toEncrypt.contains(propertyName) ? toEncryptString(propertyValue) : propertyValue ;
}
public static String getProperty(String key) {
return map.get(key);
}
private String toEncryptString(String key){
return key + "-encrypt" ;
}
}
初始化顺序
convertProperty
->
processProperties