Autowiring Map not working as expected Autowiring Map not working as expected spring spring

Autowiring Map not working as expected


3.11.3. Fine-tuning annotation-based autowiring with qualifiers:

Quote:If you intend to express annotation-driven injection by name, do not primarily use @Autowired - even if is technically capable of referring to a bean name through @Qualifier values. Instead, prefer the JSR-250 @Resource annotation which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.

As a specific consequence of this semantic difference, beans which are themselves defined as a collection or map type cannot be injected via @Autowired since type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection/map bean by unique name.


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 (a HashMap) was actually instantiated as a HashMap<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 class attribute is not used if you supply a factory-bean attribute. Have you tried leaving it out entirely?


I'm pretty sure your factory method returns java.util.Map, not java.util.HashMap, so I guess you could probably do this:

<bean class="java.util.Map" id="dienstverbandMap"      factory-bean="someFactoryMethod" factory-method="getMappedMap"/>  

Disclaimer: I'm not sure if Spring will let you do that as Map is an interface, but it's worth a try.