获取spring中配置的properties属性名和属性值

自定义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 }*****


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0X码上链

你的鼓将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值