zoukankan      html  css  js  c++  java
  • 通过spring工厂读取property配置文件

    /** 
     * 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");
     
  • 相关阅读:
    ●BZOJ 2669 [cqoi2012]局部极小值
    ●HDU 6021 MG loves string
    试试数学公式~
    ●BZOJ 3622 已经没有什么好害怕的了
    ●BZOJ 2560 串珠子
    ●BZOJ 4361 isn
    ●BZOJ 2393 Cirno的完美算数教室
    ●BZOJ 1042 [HAOI2008]硬币购物
    ●BZOJ 2839 集合计数
    【LG2481】[SDOI2011]拦截导弹
  • 原文地址:https://www.cnblogs.com/ywqbj/p/5644657.html
Copyright © 2011-2022 走看看