Spring can’t autowire Map bean

Starting with Spring 4.3, @Autowired can inject lists and maps and the given code in the question would work:

That said, as of 4.3, collection/map and array types can be matched through Spring’s @Autowired type matching algorithm as well, as long as the element type information is preserved in @Bean return type signatures or collection inheritance hierarchies.

But with a lower Spring version, you can’t autowire a collection like that. However, you can do the following:

@Resource(name="AdditionalParams")
private Map<String, String> additionalParams;

or even:

@Value("#{AdditionalParams}")
private Map<String, String> additionalParams;

Check the spring docs, the tips section:

beans that are themselves defined as a collection or map type cannot
be injected through @Autowired, because type matching is not properly
applicable to them. Use @Resource for such beans

Leave a Comment