Skip to content

Unable to autowire concrete classes when java config methods return interface [SPR-11046] #15673

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 Oct 30, 2013 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 30, 2013

Michael Minella opened SPR-11046 and commented

When using JavaConfig, if the user writes the config method as shown below:

@Bean
protected ItemWriter<Object> writer() {
	return new RetrySampleItemWriter();
}

and attempts to use that bean in a class configured as such:

@Autowire
private RetrySampleItemWriter<?> itemWriter;

it doesn't work with Spring 4. Instead, you get a NoSuchBeanDefinitionException stating that there are no beans qualifying for type RetrySampleItemWriter (RetrySampleItemWriter implements ItemWriter). This worked in Spring 3.2.


Affects: 4.0 RC1

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

We generally recommend to declare the most specific dependency as the return type of a factory method, i.e. as specific as the injection points declare it.

Nevertheless, if it worked before, it should still work. Like the other two issues, this is a side effect of our generic-type-aware autowiring algorithm...

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Fixed through only checking the method return type constraints if it is actually sufficiently expressive for the required dependency type: In other words, if the match only works against the already created singleton instance but not against the factory method return type, we skip a generic type match check for the factory method signature.

That said, please usually make the factory method return type as specific as possible. That never hurts and makes things work in case of the target bean instance not having been created yet as well.

Juergen

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: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants