Skip to content
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

@Autowired vs. @Resource [SPR-5579] #10250

Closed
spring-projects-issues opened this issue Mar 17, 2009 · 2 comments
Closed

@Autowired vs. @Resource [SPR-5579] #10250

spring-projects-issues opened this issue Mar 17, 2009 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Mar 17, 2009

Paul Bacsik opened SPR-5579 and commented

@Autowired public void setDataSource(DataSource ds) does not wire when default-autowire="true" and there exists more DataSource beans e.g with ids dataSource, someOtherDS1, someOtherDS2.

Declaring @Resource public void setDataSource(DataSource ds) works.

It seams @Autowired can't resolve the PropertyName, maybe ist searches the parameter name 'ds', but since this is a Property Setter, this is not the desired behaviour.


Affects: 2.5.6

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This is by design, actually: @Autowired does not handle bean property setters in a special way but rather resolves its argument by type and qualifiers. In your scenario, @Autowired @Qualifier("dataSource") would work since the target bean name is used as a default qualifier String then.

We do plan to revisit this for Spring 3.0, also for using the field name as a default qualifier value.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

The basic arrangement won't change in Spring 3.0 either: @Autowired will still be agnostic in terms of the methods that it operates on. We may just use the parameter name as default qualifier value. If you prefer to use property resolution specifically for single-arg setter methods following the JavaBeans naming convention, then the recommendation is indeed to use @Resource. Alternatively, simply add a @Qualifier value to your @Autowired method.

Juergen

@spring-projects-issues spring-projects-issues added status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels 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) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants