在搭配框架的时候,大多数时候都是需要配置多个properties文件, 那么这个时候,如何配置并获取文件里的内容呢?
第一种方法:写一个类,继承PropertyPlaceholderConfigurer ,然后启动的时候就加载:代码如下
package com.cmos.ngmttcontrol.util;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Decrypt the password to the properties file
*
*/
public final class PropertyConfigurer extends
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
private static final Pattern PATTERN = Pattern
.compile("\\$\\{([^\\}]+)\\}");
private static Properties props=null;
private static Map<String, String> map=new HashMap<String, String>();
//服务启动时,该方法就会执行。
protected void loadProperties(Properties props) throws IOException {
super.loadProperties(props);
PropertyConfigurer.setProps(props);
props.size();
if(props.size()>0){
String key = null;
String value = null;
for(Entry<Object, Object> entry: props.entrySet()){
key = (String) entry.getKey();
value = (String) entry.getValue();
if(key != null){
value = getdynamicValue(key);
if(value != null