接手的是老代码,对应配置的调用有两种方式,
一种是在xml文件中声明一个对PropertyPlaceholderConfigurer的继承类PropertiesLoader
<bean id="propertyPlaceholderConfigurer" class="com.artist.common.utils.PropertiesLoader"> <property name="location"> <value>classpath:env/filter-${environment}.properties</value> </property> </bean> // // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.artist.common.utils; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.NoSuchElementException; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public class PropertiesLoader extends PropertyPlaceholderConfigurer { private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class); private static Map<String, String> propertyMap; public PropertiesLoader() { } protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); propertyMap = new HashMap(); Iterator var3 = props.keySet().iterator(); while(var3.hasNext()) { Object key = var3.next(); String keyStr = key.toString(); String value = props.getProperty(keyStr); propertyMap.put(keyStr, value); } } public static String getValue(String key) { String systemProperty = System.getProperty(key); return systemProperty != null?systemProperty:(String)propertyMap.get(key); } public String getProperty(String key) { String value = getValue(key); if(value == null) { throw new NoSuchElementException(); } else { return value; } } public String getProperty(String key, String defaultValue) { String value = getValue(key); return value != null?value:defaultValue; } public static Integer getInteger(String key) { String value = getValue(key); if(value == null) { throw new NoSuchElementException(); } else { return Integer.valueOf(value); } } public static Integer getInteger(String key, Integer defaultValue) { String value = getValue(key); return value != null?Integer.valueOf(value):defaultValue; } public static Double getDouble(String key) { String value = getValue(key); if(value == null) { throw new NoSuchElementException(); } else { return Double.valueOf(value); } } public static Double getDouble(String key, Integer defaultValue) { String value = getValue(key); return Double.valueOf(value != null?Double.valueOf(value).doubleValue():(double)defaultValue.intValue()); } public static Boolean getBoolean(String key) { String value = getValue(key); if(value == null) { throw new NoSuchElementException(); } else { return Boolean.valueOf(value); } } public static Boolean getBoolean(String key, boolean defaultValue) { String value = getValue(key); return Boolean.valueOf(value != null?Boolean.valueOf(value).booleanValue():defaultValue); } } java调用:private static String SMSLIMITCOUNT= PropertiesLoader.getValue("sms.limit.count");
一种是用spring的@Value 比如
@Value("${sms.limit.count}") String smsLimitCount;
在xml配置文件中声明properties:
<context:property-placeholder location="classpath:config/application.properties,classpath:env/filter-${environment}.properties"/>
相比之下,第二种方法比较简单,只是之前写的人写法是这样的:
<context:property-placeholder location="classpath:config/application.properties"/> <context:property-placeholder location="classpath:env/filter-${environment}.properties"/>
这种写法导致evn/filter-${enviroment}.properties里面的配置文件项在java中找不到,在xml配置文件里是可以的