码迷,mamicode.com
首页 > 编程语言 > 详细

通过spring工厂读取property配置文件

时间:2016-07-05 20:42:18      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

/** 
 * Created by ywq on 2016/6/30. 
 */
@Named
public class PropertyConfig {    
	private static AbstractBeanFactory beanFactory = null;    
	private static final Map<String,String> cache = new oncurrentHashMap<>();    
	
	@Inject    
	public PropertyConfig(AbstractBeanFactory beanFactory) {        
		this.beanFactory = beanFactory;    
	}
    
	/**     
	 * 根据key获取配置文件的Value    
	 * @param key     * @return     
 	 */    
	public static String getProperty(String key) {
        	String propValue = "";
        	if(cache.containsKey(key)){
        	    propValue = cache.get(key);
        	} else {
        	    try {
                	propValue = beanFactory.resolveEmbeddedValue("${" + key.trim() + "}");
                	cache.put(key,propValue);
            	    } catch (IllegalArgumentException ex) {
                	ex.printStackTrace();
            	    }
                }
        	return propValue;
     	}
}
Spring xml的配置
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:props/${property-path}.properties</value>
                <value>classpath:important.properties</value>
            </list>
        </property>
    </bean>
 
在项目中使用
String maxTimeInSecondsProp = PropertyConfig.getProperty("maxTimeInSeconds");
 

通过spring工厂读取property配置文件

标签:

原文地址:http://www.cnblogs.com/ywqbj/p/5644657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!