使用BeanFactoryAnnotationUtils有什么不对?

时间:2015-12-24 11:04:30

标签: java spring spring-annotations

我试图简单地调用方法

  

BeanFactoryAnnotationUtils.qualifiedBeanOfType

这是项目的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>beanfactoryannotationutils.sample</groupId>
<artifactId>BeanFactoryAnnotationUtilsProblem</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.1.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
    <!-- Spring boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Application.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);

        ListableBeanFactory beanFactory = ctx.getBeanFactory();

        // This is working ... (@Autowired with @Qualifier(...) too)
        Map<String, QualifiedInterface> qualifiedBeans = beanFactory.getBeansOfType(QualifiedInterface.class);
        System.out.println("Existing qualified beans ...");
        for (QualifiedInterface qualifiedBean : qualifiedBeans.values()) {
            System.out.println(" " + qualifiedBean.toString());
            for (Annotation annotation : qualifiedBean.getClass().getAnnotations()) {
                System.out.println(" - " + annotation.toString());
            }
        }

        // This is working too ...
        Map<String, Object> qualifiedBeansFromCtx = ctx.getBeansWithAnnotation(Qualifier.class);
        System.out.println("Existing qualified beans from context ...");
        for (Object qualifiedBean : qualifiedBeansFromCtx.values()) {
            System.out.println(" " + qualifiedBean.toString());
            for (Annotation annotation : qualifiedBean.getClass().getAnnotations()) {
                System.out.println(" - " + annotation.toString());
            }
        }

        // This is not ...
        QualifiedInterface processor = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
            beanFactory, QualifiedInterface.class, "QualifierA");

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

界面 QualifiedInterface.java

public interface QualifiedInterface {

}

两个豆 QualifiedA.java

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

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

QualifiedB.java

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

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

整个在同一个包和应用程序中,现在运行它:

2015-12-24 15:08:34.468  INFO 9524 --- [           main] l.p.e.b.Application                      : Starting Application on DTBE-DEV4 with PID 9524 (D:\Workspace_netbeans\BeanFactoryAnnotationUtilsProblem\target\classes started by eguenichon in D:\Workspace_netbeans\BeanFactoryAnnotationUtilsProblem)
2015-12-24 15:08:34.478  INFO 9524 --- [           main] l.p.e.b.Application                      : No profiles are active
2015-12-24 15:08:34.618  INFO 9524 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@11e18e8c: startup date [Thu Dec 24 15:08:34 CET 2015]; root of context hierarchy
2015-12-24 15:08:37.516  INFO 9524 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-12-24 15:08:37.524  INFO 9524 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2015-12-24 15:08:37.672  INFO 9524 --- [           main] l.p.e.b.Application                      : Started Application in 3.715 seconds (JVM running for 4.355)
Existing qualified beans ...
 QualifiedA
 - @org.springframework.stereotype.Component(value=)
 - @org.springframework.beans.factory.annotation.Qualifier(value=QualifierA)
 QualifiedB
 - @org.springframework.stereotype.Component(value=)
 - @org.springframework.beans.factory.annotation.Qualifier(value=QualifierB)
Existing qualified beans from context ...
 QualifiedA
 - @org.springframework.stereotype.Component(value=)
 - @org.springframework.beans.factory.annotation.Qualifier(value=QualifierA)
 QualifiedB
 - @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:100)
    at org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils.qualifiedBeanOfType(BeanFactoryAnnotationUtils.java:56)
    at beanfactoryannotation.sample.beanfactoryannotationutilsproblem.Application.main(Application.java:45)
2015-12-24 15:08:37.684  INFO 9524 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@11e18e8c: startup date [Thu Dec 24 15:08:34 CET 2015]; root of context hierarchy
2015-12-24 15:08:37.686  INFO 9524 --- [       Thread-1] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
2015-12-24 15:08:37.691  INFO 9524 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

BeanFactoryAnnotationUtils.qualifiedBeanOfType似乎不适用于上下文中存在的限定bean,但无法检索。你知道这里的问题是什么吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

由于BeanFactoryAnnotationUtils类的默认规范,因此未在Spring 4.3之前提供。

请参阅:https://jira.spring.io/browse/SPR-13819

答案 1 :(得分:1)

在查看BeanFactoryAnnotationUtils.qualifiedBeanOfType(...)方法背后的代码时,我注意到从AbstractBeanDefinition对象检查限定符。当我在我们的例子中转储这个对象时,我可以注意到“限定符”地图是空的?!?!?

在Spring加载带有注释限定符的bean定义的方式中,它看起来像是一个错误。

已经存在一个看起来非常相似的未解决问题:https://jira.spring.io/browse/SPR-13452

我想你可以专门用你的测试用例打开另一个。