You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Autowiring inconsistency: @Qualifier works with HashMap but not with Map [SPR-17197] #21731
No qualifying bean of type 'java.util.Map<java.lang.String, java.lang.String>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=instances)}
When I change Map to HashMap<String,String> it works: no exception is thrown.
It should work using the Map interface. The @Qualifier annotation specifies the name; the bean exists.
This difference originates in Spring's special handling of collection/map interface types which might be an an aggregated set of several target beans, or a single target bean which implements the collection/map type itself. For a full generic type match, this will cleanly resolve... but for a 'raw' type match between a collection interface and a target bean with unresolved generics (since your target HashMap is a plain type with erased generics), our algorithm backs out since it does not consider those a clear enough match.
An @Bean factory method for the target collection can declare its generics in the return type signature, which is one way to make this resolve cleanly. Also, a more specific dependency type (such as HashMap) is considered a hint that a single target bean is desired even if the generics cannot be resolved. And as of 5.1, we consider qualifier matches as a clear enough hint that a bean with unresolved generics is a match in such a scenario now.
Activity
spring-projects-issues commentedon Sep 13, 2018
Juergen Hoeller commented
This difference originates in Spring's special handling of collection/map interface types which might be an an aggregated set of several target beans, or a single target bean which implements the collection/map type itself. For a full generic type match, this will cleanly resolve... but for a 'raw' type match between a collection interface and a target bean with unresolved generics (since your target
HashMapis a plain type with erased generics), our algorithm backs out since it does not consider those a clear enough match.An
@Beanfactory method for the target collection can declare its generics in the return type signature, which is one way to make this resolve cleanly. Also, a more specific dependency type (such asHashMap) is considered a hint that a single target bean is desired even if the generics cannot be resolved. And as of 5.1, we consider qualifier matches as a clear enough hint that a bean with unresolved generics is a match in such a scenario now.