它适用于我的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
<bean id="object1" class="sk.matejtymes.elTest.Object1"/>
<bean id="object2" class="sk.matejtymes.elTest.Object2"/>
<util:map id="theMap">
<entry key-ref="object1" value-ref="object2"/>
</util:map>
<bean id="elTestClass" class="sk.matejtymes.elTest.ElTestClass">
<constructor-arg index="0"
value="#{T(sk.matejtymes.elTest.ElTestClass).build(theMap)}"/>
</bean>
</beans>
并对这个测试类进行分类:
public class ElTestClass {
private Map<Object1, Object2> map;
public ElTestClass(ElTestClass orig) {
this.map = new LinkedHashMap<Object1, Object2>(orig.map);
}
private ElTestClass(Map<Object1, Object2> map) {
this.map = map;
}
public static ElTestClass build(Map<Object1, Object2> map) {
return new ElTestClass(map);
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/elTest/application-context.xml");
ElTestClass elTestClass = context.getBean(ElTestClass.class);
System.out.println(elTestClass);
}
}
注意:Object1 和 Object2 类完全为空。