Skip to content

Doc: @Autowired does not support Collection and Map as actual bean types [SPR-4492] #9170

Closed

Description

@spring-projects-issues

Bob Tiernay opened SPR-4492 and commented

The following branch in DefaultListableBeanFactory.resolveDependency requires that a dependency of interface type java.util.Map have java.lang.String as it's key type or it will fail:

...
else if (Map.class.isAssignableFrom(type) && type.isInterface()) {
Class keyType = descriptor.getMapKeyType();
if (keyType == null || !String.class.isAssignableFrom(keyType)) {
if (descriptor.isRequired()) {
throw new FatalBeanException("Key type [" + keyType + "] of map [" + type.getName() +
"] must be assignable to [java.lang.String]");
}
return null;
}
...

This seems overly restrictive and is evinced when using @Autowire. Is there some rationale for this contraint?


Affects: 2.5.1

Issue Links:

Activity

  1. spring-projects-issues commented on Feb 24, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Well, how would you expect a Map with a non-String key type to get resolved at runtime? We can only really resolve a Map with bean name as key and bean instance as value... Or are you wondering about a Map with undeclared key type while still accepting a Map with String keys at runtime?

    Juergen

  2. spring-projects-issues commented on Feb 24, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Bob Tiernay commented

    The situation I am talking about is this:

    1.A @Autowired @Component bean with a Map<SomeEnum, SomeBean> mapBean dependency identified by type and @Qualifier("mapBean")
    2. A util:map bean used as the autowired candidate bean with id="mapBean" key-class="SomeEnum", value-class="SomeBean" defined inline in xml. This map is not autowired.

    Does that clarify the context?

  3. spring-projects-issues commented on Feb 24, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    OK, so what you're trying to do there is to use @Autowired for a bean with the actual bean type being a Map. This is unfortunately not supported: @Autowired always treats arrays, Collections and Maps as sets of corresponding beans, with the target bean type derived from the declared collection value type. This is how it is semantically defined. Admittedly this isn't properly documented, so I'm turning this into a documentation issue.

    If you'd like to inject a raw Collection or Map via an annotation, then consider using the @Resource (javax.annotation.Resource) annotation instead. You could simply define a setter method for your Map and annotate it with @Resource("mapBean") - or with plain @Resource when naming your setter "setMapBean". This will always inject the specified target resource (the named Spring bean in this case) as-is, since it doesn't support any conversion or aggregation.

    Juergen

  4. spring-projects-issues commented on Feb 24, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Bob Tiernay commented

    "@Autowired always treats arrays, Collections and Maps as sets of corresponding beans, with the target bean type derived from the declared collection value type. This is how it is semantically defined. "

    Yes, the semantics you speak about are definitely news to me. Why is this the case? It seems unintuitive to me.

    Seems like requiring a @Resource annotation for this special case is inconsistent with the expectations of the user.

  5. spring-projects-issues commented on Feb 24, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Well, collection-based injection of a group of target beans is not uncommon and was one of the new general injection features in Spring 2.5. Consider Set<OrderService> for injecting all defined OrderService beans, for example. There are plenty of use cases for this. We support Map<String, OrderService> as well there (as a less common option), using the canonical bean name as key.

    That said, I agree that this is not really intuitive if you take a pure @Autowired bean injection perspective. Unfortunately it is more important to keep the semantics clean than to cover every possible use case; so I'm afraid that this won't change.

    Note that the @Qualifier(beanName) style is actually rather discouraged, since @Autowired is fundamentally about type-driven injection with optional semantic qualifiers. @Qualifier is rather meant to be used for semantic (non-unique / non-id-like) qualifier values like "EMEA" or "persistent" or the like. If you find yourself referring to concrete bean names there, you're arguably already in @Resource territory - which is injection of named beans, with the type playing no role for the resolution process. Effectively, the qualifier falling back to the bean name is a last matching resort for @Autowired, with the qualifier value still having qualifier (narrowing) semantics and not bean name (identification) semantics!

    This is also why Collection and Map are resolved by declared value type when using the @Autowired annotation: Matching the collection type itself reflectively doesn't make much sense, since any collection bean would match any collection reference here (since we can't determine the actual value type of a target collection object according to Java's generics rules). Such collection matching would absolutely require qualifiers, which is an indication that it doesn't fit with @Autowired's intention (avoiding qualifiers unless the type match itself is not unique). OTOH, determining the declared value type on the receiving side and then finding matching target beans is a straightforward fit with that intention.

    I hope that clarifies things a bit...

    Juergen

  6. spring-projects-issues commented on Feb 25, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Bob Tiernay commented

    Thanks for clarifying for me Juergen. I'm quite new to @Autowired / @Resource, so this clarifies things quite a bit.

    However, I would recommend adding something to the spring reference manual section "3.11.2. Fine-tuning annotation-based autowiring with qualifiers" indicating:

    "Note that the @Qualifier(beanName) style is actually rather discouraged, since @Autowired is fundamentally about type-driven injection with optional semantic qualifiers. @Qualifier is rather meant to be used for semantic (non-unique / non-id-like) qualifier values like "EMEA" or "persistent" or the like. If you find yourself referring to concrete bean names there, you're arguably already in @Resource territory "

    as you put it.

    Thanks again

  7. spring-projects-issues commented on Feb 26, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    I've added corresponding clarification to the reference documentation. Admittedly the section about qualifiers sort of suggested referring to bean names using @Qualifier there... I've revised this section to clarify the actual intention and corresponding semantics, including a reference to @Resource for injection by unique name.

    Juergen

  8. added
    in: core
    type: task
    on Jan 11, 2019
  9. added this to the 2.5.2 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: task

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions