java – 将动态属性映射读入Spring管理的bean

前端之家收集整理的这篇文章主要介绍了java – 将动态属性映射读入Spring管理的bean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个这样的属性文件
  1. my.properties file:
  2. app.One.id=1
  3. app.One.val=60
  4.  
  5. app.Two.id=5
  6. app.Two.val=75

我在Spring配置文件中将这些值读入我的bean中的map属性,如下所示:

  1. spring-config.xml:
  2. <bean id="myBean" class="myClass" scope="singleton">
  3. <property name="myMap">
  4. <map>
  5. <entry key="${app.One.id}" value="${app.One.val}"/>
  6. <entry key="${app.Two.id}" value="${app.Two.val}"/>
  7. </map>
  8. </property>
  9. </bean>

这样,如果我添加一个新的id / val到属性文件,我必须在config xml中添加一行,以便在myMap中有新的id / val.

我的问题是,有没有办法在spring配置文件中指定key-val对,以便xml中定义的key-val的数量可以找出属性文件中的项目并创建一个map.基本上我想在不同的环境中使用这个xml文件,我们在属性文件中使用不同数量的键值项.我只是不想在每个环境中更改xml文件以读取所有这些值.

如果您需要任何其他细节,请告诉我们任何想法/意见是赞赏.谢谢!

解决方法

这是使用Spring EL和自定义处理完成的.

尝试它是有趣的.有用 :)

application.xml中

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  7. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
  8. ">
  9.  
  10. <util:properties id="values" location="values.properties" />
  11.  
  12. <bean id="hello" class="my.Hello">
  13. <property name="map"
  14. value="#{T(my.Utils).propsToMap(values,'^(app\.\w*)\.id$','{idGroup}.val')}" />
  15. </bean>
  16.  
  17. </beans>

Utils.java

  1. package my;
  2.  
  3. import java.util.Enumeration;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Properties;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class Utils {
  11.  
  12. public static Map<String,String> propsToMap(Properties props,String idPatternString,String valuePatternString) {
  13.  
  14. Map<String,String> map = new HashMap<String,String>();
  15.  
  16. Pattern idPattern = Pattern.compile(idPatternString);
  17.  
  18. for (Enumeration<?> en = props.propertyNames(); en.hasMoreElements();) {
  19. String key = (String) en.nextElement();
  20. String mapKey = (String) props.getProperty(key);
  21.  
  22. if (mapKey != null) {
  23. Matcher idMatcher = idPattern.matcher(key);
  24. if (idMatcher.matches()) {
  25.  
  26. String valueName = valuePatternString.replace("{idGroup}",idMatcher.group(1));
  27. String mapValue = props.getProperty(valueName);
  28.  
  29. if (mapValue != null) {
  30. map.put(mapKey,mapValue);
  31. }
  32. }
  33. }
  34. }
  35.  
  36. return map;
  37. }
  38. }

Hello.java

  1. package my;
  2.  
  3. import java.util.Map;
  4.  
  5. public class Hello {
  6.  
  7. private Map<String,String> map;
  8.  
  9. public Map<String,String> getMap() {
  10. return map;
  11. }
  12.  
  13. public void setMap(Map<String,String> map) {
  14. this.map = map;
  15. }
  16. }

values.properties

  1. app.One.id=1
  2. app.One.val=60
  3.  
  4. app.Two.id=5
  5. app.Two.val=75

猜你在找的Java相关文章