配置文件讀寫

#datasource.key
datasource.key=GS,QH


#referer.url
GS=http://10.139.8.38:7100/cam/lanzhou/cm/,http://10.139.8.38:7100/cam/gansu/cm/,http://10.139.8.141/cam/yxt/cm/

QH=http://10.139.8.38:7100/cam/qinghai/cm/,http://10.139.8.38:7100/cam/xining/cm/,http://10.139.8.141/cam/lh/cm/


@Repository
public class DataSourceKeyValueMap {


@Value("${datasource.key}")
private String dataSourceKey;


@Resource
CustomizedPropertyPlaceholderConfigurer cpc;


private static Map<String, String> dataSourceKeyMap;


private void init() {
dataSourceKeyMap = new HashMap<String, String>(100);
final String[] keyArray = dataSourceKey.split(",");
final int arrayLength = keyArray.length;
for (int i = 0; i < arrayLength; i++) {
final String refererUrl = cpc.getContextProperty(keyArray[i]);
final String[] urlArray = refererUrl.split(",");
final int urlArrayLength = urlArray.length;
for (int j = 0; j < urlArrayLength; j++) {
dataSourceKeyMap.put(urlArray[j], keyArray[i]);
}
}
}


public Map<String, String> getDataSourceKeyMap() {
if (dataSourceKeyMap == null) {
init();
}
return dataSourceKeyMap;
}


}


public class CustomizedPropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {


private static Properties staticProperties;


@Override
protected void processProperties(
final ConfigurableListableBeanFactory beanFactory,
final Properties props) throws BeansException {


super.processProperties(beanFactory, props);
staticProperties = props;


}


// static method for accessing context properties
public String getContextProperty(final String key) {
return staticProperties.getProperty(key);
}


}


<!-- spring的屬性加載器,加載properties文件中的屬性 -->
<bean id="propertyConfigurer"
class="com.chinamobile.cmss.cam.cs.utils.cache.CustomizedPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:common.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章