我有一个这样的属性文件:
my.properties file:
app.One.id=1
app.One.val=60
app.Two.id=5
app.Two.val=75
我将这些值读取到Spring配置文件中bean的map属性中,如下所示:
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>
这样,如果我向属性文件添加一个新的id/val,我必须在config.xml中添加一行,以便在myMap中具有新的id/val。
我的问题是,是否有一种方法可以在spring配置文件中指定键值对,以便在xml中定义的键值数量可以计算出属性文件中的项并创建映射。基本上,我想在不同的环境中使用这个xml文件,在这些环境中,我们在属性文件中使用不同数量的键值项。我只是不想更改每个环境中的xml文件来读取所有这些值。
如果你需要任何其他细节,请告诉我。如有任何想法/意见,我们将不胜感激。谢谢
这是通过Spring EL和自定义处理完成的。
尝试一下对我来说很有趣。它很有效:)
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;
}
}
值。属性
app.One.id=1
app.One.val=60
app.Two.id=5
app.Two.val=75
也许我没有完全理解这里的问题
简化的方法怎么样
my.properties file:
1=60
5=75
应用程序上下文
<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath: my.properties</value>
</list>
</property>
</bean>
<bean id="myClass" class="com.pakage.MyClass">
<property name="myMap" ref=" myProperties"/>
</bean>
Java Bean
public class MyClass {
private Map<String , String> myMap;
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public Map<String , String> getMyMap(){
return myMap;
}
}
这是一个经典的环境问题。
有两种方法可以做到这一点:
- 在相应的.properties文件末尾添加一个环境字符串;在应用程序启动时将该值传递给它,并让Spring选择正确的值
- 将这些动态属性放入数据库中,并在启动时查询它们。数据库的JNDI将选择正确的值
我没有找到一种理想的方法来解决这个问题,将动态映射属性读取到spring配置信息中。使用DB也不是我们的选择。这是我发现的最好的选择:
-
使用特性文件中映射键/值对的标准格式例如:key1|val1,key2|val2,key3|val3。。。,凯恩斯
-
将其读取到配置bean中的String属性或List属性中,如下所示:使用String列出
-
让注入的java类在setter中将项目拆分为map。
我将把这个标记为答案。如果其他人有更好的方法来做这件事,请评论出来,我会把它改成答案。