Skip to content

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

Closed

Description

@spring-projects-issues

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:

Activity

  1. spring-projects-issues commented on Oct 30, 2013

    @spring-projects-issues
    CollaboratorAuthor

    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

  2. spring-projects-issues commented on Oct 30, 2013

    @spring-projects-issues
    CollaboratorAuthor

    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

  3. added
    type: bug
    in: core
    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: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions