统一管理properties

来源:互联网 发布:modern php百度盘 编辑:程序博客网 时间:2024/06/11 21:15

在系统中有多个properties时,可以用下面的方面。统一引用 

package com.hoolai.fastaccess.common.springextend;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 CustomizedPropertyPlaceholderConfigurer extendsPropertyPlaceholderConfigurer {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);//这里是为了在web.xml里用${tcConfigUrl} filter要设置在后面启动。不能随便set。会覆盖系统的property    lizhiqiangif(keyStr.equalsIgnoreCase("tcConfigUrl")) {System.setProperty(keyStr,value);}ctxPropertiesMap.put(keyStr, value);}}public static Object getContextProperty(String name) {return ctxPropertiesMap.get(name);}public static String getString(String name) {return (String)ctxPropertiesMap.get(name);}}

在mvc.xml或其他xml里可以这样写:

<bean id="propertyConfigurer"class="com.hoolai.fastaccess.common.springextend.CustomizedPropertyPlaceholderConfigurer"><property name="ignoreUnresolvablePlaceholders" value="true" /><property name="locations"><list><value>classpath:${env.dir}/upload.properties</value><value>classpath:${env.dir}/dubbo.properties</value><value>classpath:${env.dir}/jdbc.properties</value><value>classpath:${env.dir}/packaging.properties</value><value>classpath:${env.dir}/terracotta.properties</value></list></property></bean>

这样,在需要引用的地方,可以这样用 

String sdkPath = (String) CustomizedPropertyPlaceholderConfigurer.getContextProperty("sdk.path") + "/" + channelInfo.getChannel() + "_fastaccess_sdk.apk";


0 0
原创粉丝点击