I’m using Spring 3.0.4. I have some beans that use the @Autowired annotation on Maps. These maps are defined within an application-context.xml file (as these maps are constructed using several factory methods).
When I use my debugger, I can see the map gets constructed using the properly (expected) bean id. However, once the autowiring process starts, it claims it cannot find a bean with the id that just has been created.
Piece of code:
@Autowired
@Qualifier("dienstverbandMap")
private Map<String, String> dienstverbandMap;
Piece of context xml:
<bean class="java.util.HashMap" id="dienstverbandMap" factory-bean="someFactoryMethod" factory-method="getMappedMap"/>
Important detail, when I change the type to java.lang.Object in both my Class and the context xml it does get wired In fact, I can cast it to a HashMap in my code and get everything to work. But that is not what i want obviously.
Anyone got an explantion what I’m doing wrong?
I think this is something to do with the type parameters for
dienstverbandMap. The injection can only be performed safely if Spring can figure out that the bean instance (aHashMap) was actually instantiated as aHashMap<String, String>. Spring could be losing the type parameters because of the bean’s declared type is a raw type.Another possibility is that the result signature of the factory method is wrong; e.g. Map instead of HashMap, or a raw HashMap rather than a
HashMap<String, String>.(Some of these theories could be disproved if you showed us the declaration of the factory method.)
By the way, according to the comments in the spring-beans 2.0 DTD and 3.0 XSD, the
classattribute is not used if you supply afactory-beanattribute. Have you tried leaving it out entirely?