<bean id="dnsConfig" class="com.configuration.ParameterConfig"> <property name="valueStack"> <map> <entry key="js"> <value>http://js.imcoal.com</value> </entry> <entry key="js100M"> <value>http://218.21.222.164</value> </entry> <entry key="js10M"> <value>http://110.19.108.84</value> </entry> </map> </property> </bean>
ParameterConfig是一个参数配置类
public class ParameterConfig extends AbstractConfig<String> {
}
AbstractConfig类
public abstract class AbstractConfig<V> implements Configuration<String, V>{
protected final Log logger = LogFactory.getLog(this.getClass());
private Map<String , V> valueStack = Collections.synchronizedMap(new HashMap<String, V>());
public V getValue(String key) {
return valueStack.get(key);
}
public void putValue(String key, V value) {
valueStack.put(key, value);
}
public V getCurType (String curType) {
return null;
}
public Map<String, V> getValueStack() {
return valueStack;
}
public void setValueStack(Map<String, V> valueStack) {
this.valueStack = valueStack;
}
protected boolean containsKey(String key) {
return valueStack.containsKey(key);
}
}
Configuration接口
public interface Configuration <K, V>{
public abstract V getValue(K key);
public abstract void putValue(K key, V value);
public abstract V getCurType(K key);
}
Spring容器启动时自动加载配置文件配置的值,放到map中,
使用
// 请求发起方判断
ApplicationContext context = WebApplicationContextUtils
.getRequiredWebApplicationContext(session.getServletContext());
ParameterConfig dnsConfig = (ParameterConfig)
context.getBean("dnsConfig");
for (String key : dnsConfig.getValueStack().keySet()) {
System.out.println("value:" + dnsConfig.getValue(key));
}