@MrXiao
2017-12-19T03:45:22.000000Z
字数 2762
阅读 1318
Spring Properties
将一些业务常量配置成properties文件,是扩展程序兼容性的常用做法。在业务变更导致数据需要改变时,不需要修改代码,只用更改配置文件并重启即可生效。
public static String getValue(String fileNamePath, String key) throws IOException {Properties props = new Properties();InputStream in = null;try {in = new FileInputStream(fileNamePath);//如果将in改为下面的方法,必须要将.Properties文件和此class类文件放在同一个包中//in = propertiesTools.class.getResourceAsStream(fileNamePath);props.load(in);String value = props.getProperty(key);// 有乱码时要进行重新编码// new String(props.getProperty("name").getBytes("ISO-8859-1"), "GBK");return value;} catch (FileNotFoundException e) {return null;} finally {if (null != in) {in.close();}}}
在spring扫描配置文件时,将properties中的key和value放入一个map中。
Spring配置
<bean id="propertyConfigurer" class="com.hapishop.util.ProjectDBinfoConfigurer"><property name="locations"><list><value>classpath*:/META-INF/*.properties</value><value>file:conf/*.properties</value></list></property></bean>
自定义CustomizedPropertyConfigurer,继承PropertyPlaceholderConfigurer
public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {private static Map<String, String> properties = new HashMap<String, String>();protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {// cache the propertiesPropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(DEFAULT_PLACEHOLDER_PREFIX,DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);for (Entry<Object, Object> entry : props.entrySet()) {String stringKey = String.valueOf(entry.getKey());String stringValue = String.valueOf(entry.getValue());stringValue = helper.replacePlaceholders(stringValue, props);properties.put(stringKey, stringValue);}super.processProperties(beanFactoryToProcess, props);}public static Map<String, String> getProperties() {return properties;}public static String getProperty(String key) {return properties.get(key);}}
配置properties
site=iteyeblog=antloveurl=${site}/${blog}
Java获取
//调用此方法获取valueCustomizedPropertyConfigurer.getContextProperty()
Spring配置
<bean id="propertyConfigurer" class="com.hapishop.util.ProjectDBinfoConfigurer"><property name="locations"><list><value>classpath*:/META-INF/*.properties</value><value>file:conf/*.properties</value></list></property></bean>
注解获取
@Value("${socket.time.out}")int socketTimeout;public void setSocketTimeout(int socketTimeout) {this.socketTimeout = socketTimeout;}
@Value写在要注入的属性上,要提供setter方法。
在java 代码里,一般是使用@Value注解来引用 properties 文件的属性。
使用 PropertyPlaceholderConfigurer 时, @Value表达式的用法是 @Value(value="${properties key}") ,
使用 PropertiesFactoryBean 时,我们还可以用@Value 读取 properties对象的值, @Value 用法 是 @Value(value="#{configProperties['properties key']}")