自定义PropertyPlaceholderConfigurer:
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;
public class SpringPropertyConfigurer extends PropertyPlaceholderConfigurer {
private static Map<String, Object> ctxPropertiesMap;
@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 Map<String, Object> getCtxPropertiesMap() {
return ctxPropertiesMap;
}
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
}
applicationContext.xml加载:
<bean id="propertyConfigurer" class="com.midea.jr.credit.accounting.utils.SpringPropertyConfigurer" lazy-init="false">
<property name="locations">
<list>
<value>classpath:system.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8" />
</bean>
读取方式:
private SpringPropertyConfigurer propertyConfigurer ;
@Override
public void setServletContext( ServletContext servletContext ) {
Map<String, Object> mValue = propertyConfigurer.getCtxPropertiesMap();
for(String key : mValue.keySet()){
LOGGER.debug( "***** Config application param :{ " + key + ":" + mValue.get( key ) + " }*****" );
if(key.startsWith( "log" )){
System.setProperty( key, mValue.get( key ).toString() );
}
}
输出
2016-07-06 17:44:33,529 - -870 [localhost-startStop-1] DEBUG - ***** Config application param :{ jdbc.acquireCheckoutTimeout:3000 }*****
2016-07-06 17:44:33,529 - -870 [localhost-startStop-1] DEBUG - ***** Config application param :{ redis.pool.testOnBorrow:true }*****
2016-07-06 17:44:33,529 - -870 [localhost-startStop-1] DEBUG - ***** Config application param :{ redis.pool.maxIdle:100 }*****
2016-07-06 17:44:33,529 - -870 [localhost-startStop-1] DEBUG - ***** Config application param :{ redis.pool.maxActive:300 }*****
2016-07-06 17:44:33,529 - -870 [localhost-startStop-1] DEBUG - ***** Config application param :{ jdbc.maxIdleTime:2000 }*****
2016-07-06 17:44:33,529 - -870 [localhost-startStop-1] DEBUG - ***** Config application param :{ redis.pass:1234xxxxx }*****
2016-07-06 17:44:33,529 - -870 [localhost-startStop-1] DEBUG - ***** Config application param :{ dubbo.protocol.port:20880 }*****
2016-07-06 17:44:33,529 - -870 [localhost-startStop-1] DEBUG - ***** Config application param :{ redis.pool.port:6379 }*****
2016-07-06 17:44:33,529 - -870 [localhost-startStop-1] DEBUG - ***** Config application param :{ jdbc.maxPoolSize:20 }*****
2016-07-06 17:44:33,529 - -870 [localhost-startStop-1] DEBUG - ***** Config application param :{ redis.maxActive:300 }*****
2016-07-06 17:44:33,529 - -870 [localhost-startStop-1] DEBUG - ***** Config application param :{ redis.port:6379 }*****