I recently switched from using Spring's XML configuration to its Java configuration and am encountering a strange issue.
The XML configuration was:
<util:map id="myMap">
<entry key="a" value="aValue"/>
<entry key="b" value="bValue"/>
<entry key="c" value="cValue"/>
</util:map>
<bean id="myBean" class="my.MyClass">
<property name="myMap" ref="myMap"/>
</bean>
The Java configuration is:
@Bean
public Map<String, Object> myMap() {
Map<String, Object> myMap = new HashMap<>();
myMap.put("a", "aValue");
myMap.put("b", "bValue");
myMap.put("c", "cValue");
return myMap;
}
@Bean
public MyClass myBean(@Qualifier("myMap") final Map<String, Object> myMap) {
MyClass myBean = new MyClass();
myBean.setMyMap(myMap);
return myBean;
}
Both beans are declared in different files, I grouped them here to make it easier to read. The map contains references too, not only strings.
I would expect to be able to use myMap
in the second bean but instead Spring injects the following map:
{ myMap = { a=aValue, b=bValue, c=cValue } }
I don't understand why Spring wraps the map into another map, and why it doesn't behave the same way with the XML configuration.
Any ideas?