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

BeanFactoryAnnotationUtils.qualifiedBeanOfType does not work for @Qualifier on bean class [SPR-13819] #18392

Closed
spring-projects-issues opened this issue Dec 24, 2015 · 2 comments
Assignees
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

spring-projects-issues commented Dec 24, 2015

Emilien Guenichon opened SPR-13819 and commented

The BeanFactoryAnnotationUtils.qualifiedBeanOfType method doesn't match the qualified bean even with the good qualifier given and interface.

As you can see from the StackOverflow link.
And from an execution of the attached project.

QualifiedInterface processor = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
beanFactory, QualifiedInterface.class, "QualifierA");

System.out.println("The selected processor should be A " + processor);

An interface QualifiedInterface.java

public interface QualifiedInterface {

}
Two beans QualifiedA.java

@Component
@Qualifier("QualifierA")
public class QualifiedA implements QualifiedInterface {

@Override
public String toString() {
    return "QualifiedA";
}

}

and QualifiedB.java :

@Component
@Qualifier("QualifierB")
public class QualifiedB implements QualifiedInterface {

@Override
public String toString() {
    return "QualifiedB";
}

}

Here a log of the execution (care about qualifier names and case as I tried to lower them between the stackoverflow tickets and this JIRA)

2015-12-24 16:14:50.445 INFO 9852 --- [ main] b.sample.Application : Starting Application on DTBE-DEV4 with PID 9852 (D:\Workspace_netbeans\BeanFactoryAnnotationUtilsProblem\target\classes started by eguenichon in D:\Workspace_netbeans\BeanFactoryAnnotationUtilsProblem)
2015-12-24 16:14:50.449 INFO 9852 --- [ main] b.sample.Application : No active profile set, falling back to default profiles: default
2015-12-24 16:14:50.535 INFO 9852 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5390ecf8: startup date [Thu Dec 24 16:14:50 CET 2015]; root of context hierarchy
2015-12-24 16:14:53.251 INFO 9852 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2015-12-24 16:14:53.266 INFO 9852 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2015-12-24 16:14:53.485 INFO 9852 --- [ main] b.sample.Application : Started Application in 3.662 seconds (JVM running for 4.154)
Existing qualified beans ...
qualifiera

  • @org.springframework.stereotype.Component(value=)
  • @org.springframework.beans.factory.annotation.Qualifier(value=qualifiera)
    qualifierb
  • @org.springframework.stereotype.Component(value=)
  • @org.springframework.beans.factory.annotation.Qualifier(value=qualifierb)
    Existing qualified beans from context ...
    qualifiera
  • @org.springframework.stereotype.Component(value=)
  • @org.springframework.beans.factory.annotation.Qualifier(value=qualifiera)
    qualifierb
  • @org.springframework.stereotype.Component(value=)
  • @org.springframework.beans.factory.annotation.Qualifier(value=qualifierb)
    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'qualifiera' is defined: No matching QualifiedInterface bean found for qualifier 'qualifiera' - neither qualifier match nor bean name match!
    at org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils.qualifiedBeanOfType(BeanFactoryAnnotationUtils.java:98)
    at org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils.qualifiedBeanOfType(BeanFactoryAnnotationUtils.java:55)
    at beanfactoryannotationutils.sample.Application.main(Application.java:45)
    2015-12-24 16:14:53.496 INFO 9852 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5390ecf8: startup date [Thu Dec 24 16:14:50 CET 2015]; root of context hierarchy
    2015-12-24 16:14:53.498 INFO 9852 --- [ Thread-1] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0
    2015-12-24 16:14:53.503 INFO 9852 --- [ Thread-1] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

Affects: 4.2.4

Reference URL: http://stackoverflow.com/questions/34451782/whats-wrong-within-the-usage-of-beanfactoryannotationutils

Attachments:

Issue Links:

Referenced from: commits 1d8a3e1

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Due to its origin (i.e. transaction manager selection and async executor selection), BeanFactoryAnnotationUtils historically just deals with qualifier values on explicit bean definitions and with analogous qualifier annotations on @Bean factory methods. It simply does not honor qualifier annotations on bean classes at all since those do not occur in the original scenarios that motivated this utility class in the first place.

Of course, arguably BeanFactoryAnnotationUtils should be extended to cover that variant as well, bringing it in sync with QualifierAnnotationAutowireCandidateResolver's algorithm for injection point matching. I've turned this issue into a corresponding feature ticket, to be addressed for 4.3.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This will be available in the upcoming 4.3.0.BUILD-SNAPSHOT... Feel free to give it an early try!

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

No branches or pull requests

2 participants