将动态属性映射读入Spring管理的bean [英] Reading a dynamic property map into Spring managed bean
问题描述
我有一个这样的属性文件:
my.properties文件:
/ pre>
app.One.id = 1
app.One.val = 60
app.Two.id = 5
app.Two.val = 75
我在Spring配置文件中将这些值读入我的bean中的map属性,如下所示:
spring-config.xml:
< bean id =myBeanclass =myClassscope =singleton>
< property name =myMap>
< map>
< entry key =$ {app.One.id}value =$ {app.One.val}/>
< entry key =$ {app.Two.id}value =$ {app.Two.val}/>
< / map>
< / property>
< / bean>
这样,如果我添加一个新的id / val到属性文件,我必须添加一行配置xml以便在myMap中有新的id / val。
我的问题是,是否有一种方法可以在spring配置文件中指定key-val对,以便在xml中定义的键值的数量可以找出属性文件中的项目并创建一个映射。基本上我想在不同的环境中使用这个xml文件,我们在属性文件中使用不同数量的键值项。我只是不想在每个环境中更改xml文件以读取所有这些值。
如果您需要任何其他详细信息,请告诉我们。任何想法/意见是赞赏。谢谢!
解决方案这是使用Spring EL和自定义处理完成的。
<对我来说,尝试它是有趣的。它的作用是:)
application.xml
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns: context =http://www.springframework.org/schema/context
xmlns:util =http://www.springframework.org/schema/utilxmlns:p =http:// www .springframework.org / schema / p
xsi:schemaLocation =
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring -beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http: //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
>
< util:properties id =valueslocation =values.properties/>
< bean id =helloclass =my.Hello>
< property name =map
value =#{T(my.Utils).propsToMap(values,'^(app\.\w *)\.id $' ,'{idGroup} .val')}/>
< / bean>
< / beans>
Utils.java
打包我的;
import java.util.Enumereration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Utils {
public static Map< String,String> propsToMap(属性道具,
String idPatternString,String valuePatternString){
Map< String,String> map = new HashMap< String,String>();
模式idPattern = Pattern.compile(idPatternString); (枚举<?> en = props.propertyNames(); en.hasMoreElements();){
String key =(String)en.nextElement();
String mapKey =(String)props.getProperty(key);
如果(mapKey!= null){
Matcher idMatcher = idPattern.matcher(key);
if(idMatcher.matches()){
String valueName = valuePatternString.replace({idGroup},
idMatcher.group(1));
String mapValue = props.getProperty(valueName);
if(mapValue!= null){
map.put(mapKey,mapValue);
}
}
}
}
返回地图;
}
}
Hello.java / p>
打包我的;
import java.util.Map;
public class Hello {
private Map< String,String>地图;
公共地图< String,String> getMap(){
return map;
}
public void setMap(Map< String,String> map){
this.map = map;
}
}
values.properties / p>
app.One.id = 1
app.One.val = 60
应用.Two.id = 5
app.Two.val = 75
I have a properties file like this:
my.properties file: app.One.id=1 app.One.val=60 app.Two.id=5 app.Two.val=75
And I read these values into a map property in my bean in Spring config file like this:
spring-config.xml: <bean id="myBean" class="myClass" scope="singleton"> <property name="myMap"> <map> <entry key="${app.One.id}" value="${app.One.val}"/> <entry key="${app.Two.id}" value="${app.Two.val}"/> </map> </property> </bean>
This way if I add a new id/val to the properties file, I must add a row in config xml in order to have the new id/val in myMap.
My question is, is there a way to specify the key-val pairs in spring config file so that the number of key-vals defined in xml can figure out the items in the properties file and create a map. Basically I want to use this xml file in different environments where we use different number of key-value items in properties file. I just don't want to change the xml file in each environment to read in all these values.
Let me know if you need any other details. Any thoughts/comments is appreciated. Thanks!
解决方案This is done with Spring EL and custom handling.
It was just interesting for me to try it. It works :)
application.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd "> <util:properties id="values" location="values.properties" /> <bean id="hello" class="my.Hello"> <property name="map" value="#{T(my.Utils).propsToMap(values, '^(app\.\w*)\.id$', '{idGroup}.val')}" /> </bean> </beans>
Utils.java
package my; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Utils { public static Map<String, String> propsToMap(Properties props, String idPatternString, String valuePatternString) { Map<String, String> map = new HashMap<String, String>(); Pattern idPattern = Pattern.compile(idPatternString); for (Enumeration<?> en = props.propertyNames(); en.hasMoreElements();) { String key = (String) en.nextElement(); String mapKey = (String) props.getProperty(key); if (mapKey != null) { Matcher idMatcher = idPattern.matcher(key); if (idMatcher.matches()) { String valueName = valuePatternString.replace("{idGroup}", idMatcher.group(1)); String mapValue = props.getProperty(valueName); if (mapValue != null) { map.put(mapKey, mapValue); } } } } return map; } }
Hello.java
package my; import java.util.Map; public class Hello { private Map<String, String> map; public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } }
values.properties
app.One.id=1 app.One.val=60 app.Two.id=5 app.Two.val=75
这篇关于将动态属性映射读入Spring管理的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!