Skip to content

@Autowired and @Resource = autowiring by type and autowiring by name? [SPR-3646] #8327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Jul 3, 2007 · 2 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Henri Karapuu opened SPR-3646 and commented

The naming of @Autowired and @Resource annotations does not make any sense.

One autowires by type, other by name, but the annotation names do not reflect this in any way, and are even of completely different grammatical form.

My suggestion would be to add attribute to the @Autowired annotation, which would set the autowire type to 'name', 'type' or 'autodetect'. If the default would be 'type' then full backwards compatibility would maintained. This way those of us who don't care about commons annotations could use @Autowired for everything, and have much cleaner looking code. Plus, we would get the added benefit of autodetecting the type.

The @Resource could be kept as is for those who want to be compatible with commons annotations.


No further details from SPR-3646

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

You do have a point there. However, @Autowired is not really a by-type version of @Resource. In general, the differentiation between the good old autowiring by type and autowiring by name tends to blur when using annotations...

Note that @Autowired is a general annotation which may be applied to constructors, config methods with an arbitrary number of arguments as well as to fields. Since constructor arguments and method arguments are unnamed at runtime, the only option there is autowiring by type. For that reason, we cannot simply introduce an "autowire mode" flag on @Autowired; the annotation rather needs to do its best to determine a target bean by type.

We plan to extend @Autowired's capabilities for Spring 2.1 RC1, supporting 'qualifiers' that differentiate between different matching target beans. An obvious option would be to base the decision on the presence of 'qualifier' annotations on the constructor argument / method argument / field, with specific target beans marked with specific qualifier values. The documentation should probably discuss the entire topic then, comparing those annotation semantics to general autowiring by type / name.

@Resource, on the other hand, is essentially an annotation shortcut for a programmatic lookup by name. It's not really Spring's good old autowiring by name, because there is no general convention applied to an arbitrary number of properties here. It's rather just specific annotated fields / setters injected with a specific named target bean, with the name explicitly specified or defaulting to the field name or bean property name. @Resource is more or less a <property ... ref="..."> tag expressed in an annotation (with some defaulting).

In other words, @Resource is for cases where the specific name (of a named 'resource') is the primary means of identifying a target, whereas @Autowired is for cases where the focus is on the characteristics of a target (i.e. its type and its qualifiers). The former is mainly for JSR-250 compatibility; the latter is what we generally recommend for annotation-driven injection purposes within business components (where the Java code should not refer to concrete component names).

I'm keeping this issue open to track Spring 2.1 RC1's state of things in terms of @Autowired semantics and the documented recommendations.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Mark Fisher commented

The @Autowired annotation now works in conjunction with qualifiers. Spring 2.1-M4 (or the most recent nightly snapshot) provides a @Qualifier annotation which may be used directly with a bean name to be wired such as: @Qualifier("someBean"). Alternatively, it is possible to annotate your own custom annotations with Spring's @Qualifier in order to use such annotations as qualifiers. Custom qualifier annotations may be simple "markers" without any attributes, or they may include any number of attributes - all of which must be matched by a bean definition in order for that bean to be considered an autowire candidate.

The schema for Spring beans now supports <qualifier/> elements. They may contain 'type' and 'value' attributes as well as sub-elements to match against the qualifier annotation attributes (such as: <attribute name='..' value='..'/>).

To review the official documentation of this new functionality (and more), please consult the latest version of the reference documentation (beginning with nightly snapshot #201). The primary coverage is in the section entitled "Fine-tuning annotation-based autowiring with qualifiers" within the "beans" chapter.

Mark

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 2.1 M4 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant