Skip to content

Qualified beans are not autowired [SPR-17056] #21594

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 18, 2018 · 1 comment
Closed

Qualified beans are not autowired [SPR-17056] #21594

spring-projects-issues opened this issue Jul 18, 2018 · 1 comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another

Comments

@spring-projects-issues
Copy link
Collaborator

Thomas OLIVIERI opened SPR-17056 and commented

Hello,

When defining multiple beans with the same qualifier, I expect the beans to end up in a list that can be autowired. It all works fine until a certain (weird) point.

 I have a bean called MyBean:

public MyBean(String name, Function<String, String> function) {
    this.name = name;
    this.function = function;
}

 I configure multiple beans of the same type (see: RootConfiguration.java):

@Bean
@Qualifier("myBean")
MyBean myBean1() {
    return new MyBean("bean1", this::functionMyBean1);
}

// ...

private String functionMyBean1(String in) {
    return in;
}

// ...

 Then I can retrieve them all using:

@Autowired
private List<MyBean> myBeans;

 The weird issue is that if I overload the bean creation method to define the function passed as the constructor parameter, then the bean is not added to the autowired list:

@Bean
@Qualifier("myBean")
MyBean myBean1() {
    return new MyBean("bean1", this::myBean1);
}

private String myBean1(String in) { // overloaded method -> bean is not added
    return in;
}

 That's not a big issue since the workaround is obvious but I really would like to understand what's going on behind the scenes so the DI does not work as expected.
Note that the bean that is not injected in the list is eventually created, but never acknowledged as part of its "siblings".

Thanks in advance for your insight !


Affects: 5.0.5

Reference URL: https://github.com/tdolivieri/spring-qualified-beans

@spring-projects-issues spring-projects-issues added type: bug A general bug status: waiting-for-triage An issue we've not yet triaged or decided on in: core Issues in core modules (aop, beans, core, context, expression) and removed type: bug A general bug labels Jan 11, 2019
@snicoll
Copy link
Member

snicoll commented Sep 18, 2023

Thanks for the sample. The weirdness that you see is indeed confusing and we've decided to deprecate it, see #31073 for more details.

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Sep 18, 2023
@snicoll snicoll added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 18, 2023
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: superseded An issue that has been superseded by another
Projects
None yet
Development

No branches or pull requests

2 participants