Skip to content

Autowiring inconsistency: @Qualifier works with HashMap but not with Map [SPR-17197] #21731

Closed

Description

@spring-projects-issues

Manfred Quasten opened SPR-17197 and commented

bean definition:

<bean id="instances" class="java.util.HashMap" >
 <constructor-arg>
 <map key-type="java.lang.String" value-type="java.lang.String">
 <entry key="0001" value="Beschreibung " />
...
 </map>
 </constructor-arg>
</bean>
public InstanceRepositoryImpl {

@Autowired
 InstanceRepositoryImpl(@Qualifier("instances") final Map<String,String> instances ) {
 this.instances= instances;
 }

}

 

Error Message:

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.


Affects: 5.0.3

Issue Links:

Activity

  1. spring-projects-issues commented on Sep 13, 2018

    @spring-projects-issues
    CollaboratorAuthor

    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 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.

  2. added
    type: enhancement
    in: core
    on Jan 11, 2019
  3. added this to the 5.1 GA milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions