当前位置: 首页 > 知识库问答 >
问题:

Spring4.1@Qualifier不起作用

上官英哲
2023-03-14

我有类,xml配置文件和错误堆栈跟踪像这样。我不知道为什么资格赛不起作用。我看到他甚至什么也没做。

public class SimpleDog implements Dog {
    @Autowired
    @Qualifier("small")
    private Size size;
    private String name;

public Size getSize() {
    return size;
}

public void setSize(Size size) {
    this.size = size;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public void giveSound() {
    System.out.println("dog size is : width : (" + size.getWidth() + ") , height : (" + size.getHeight() + ")");
    System.out.println("dog's name : " + name);
}

}

测试课程

public class Test2 {
public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-test2.xml");
    SimpleDog dog = (SimpleDog) context.getBean("dog");
    dog.giveSound();
}

}

Springxml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dog" class="com.tests.test2.SimpleDog">
         <property name="name" value="Puppy" />
    </bean>

    <bean id="size1" class="com.tests.test2.Size">
        <qualifier value="small"/>
        <property name="height" value="2"/>
        <property name="width" value="1"/>
    </bean>

    <bean id="size2" class="com.tests.test2.Size">
        <qualifier value="large"/>
        <property name="height" value="20"/>
        <property name="width" value="10"/>
    </bean>

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>

错误堆栈跟踪

WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dog': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.tests.test2.Size com.tests.test2.SimpleDog.size; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.tests.test2.Size] is defined: expected single matching bean but found 2: size1,size2
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
    at pl.patrykgryta.test2.Test2.main(Test2.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.tests.test2.Size com.tests.test2.SimpleDog.size; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.tests.test2.Size] is defined: expected single matching bean but found 2: size1,size2
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:555)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 18 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.tests.test2.Size] is defined: expected single matching bean but found 2: size1,size2
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1016)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:527)
    ... 20 more

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dog': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.tests.test2.Size com.tests.test2.SimpleDog.size; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.tests.test2.Size] is defined: expected single matching bean but found 2: size1,size2
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
    at pl.patrykgryta.test2.Test2.main(Test2.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.tests.test2.Size com.tests.test2.SimpleDog.size; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.tests.test2.Size] is defined: expected single matching bean but found 2: size1,size2
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:555)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 18 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.tests.test2.Size] is defined: expected single matching bean but found 2: size1,size2
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1016)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:527)
    ... 20 more

需要帮忙吗

共有3个答案

宣望
2023-03-14

@限定符(name=“…”)注释,并给出希望Spring注入的bean的名称

您的bean的名称是size1和size2。

所以试试看

   @Qualifier("size1")
连晟
2023-03-14

@限定符用于通过bean的名称或id引用bean。因为它找不到名称或id为“小”的xml条目,所以它试图按类型匹配,其中它找到了两个大小实例。

以下将起作用:

  <bean id="small" class="com.tests.test2.Size">      
    <property name="height" value="2"/>
    <property name="width" value="1"/>
</bean>

尽管看起来您希望将大小的实例视为预配置的bean。如果是这种情况,您可以在xml文件中声明Dog的实例并引用大小bean。。。大概是这样的:

  <bean id="rex" class="com.tests.test2.SimpleDog">
     <property name="name" value="Puppy" />
     <property name="size" ref="size1"/>
  </bean>
和光启
2023-03-14

可能您的应用上下文的BeanFactory配置为默认的(即SimpleAutowireCandidateResolver),而不是QualifierAnnotationAutowireCandidateResolver。

在Spring的早期版本(4.0之前)中,限定符AnnotationAutoWireCandidateResolver是在创建大多数ApplicationContext时设置的(比较AbstractRefreshableApplicationContext.customizeBeanFactory(DefaultListableBeanFactory)实现)。

目前,AppCtx的QualifierAnnotationAutowireCandidateResolver可以通过以下方式应用:

>

  • org。springframework。豆。工厂注释。CustomAutowireConfigurer(请参阅javadoc和后处理BeanFactory(ConfigurableListableBeanFactory)方法的实现),例如,添加到xml:

    <bean id="customAutowireConfigurer" class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
    <property name="customQualifierTypes">
        <set>
            <value>org.springframework.beans.factory.annotation.Qualifier</value>
        </set>
    </property>
    

    <代码>

  •  类似资料:
    • 问题内容: 我有这样的类,xml配置文件和错误堆栈跟踪。我不知道为什么@Qualifier无法正常工作。我看到他甚至什么都不做的错误。 狗 } 考试班 } spring.xml 错误堆栈跟踪 需要帮忙 问题答案: @Qualifier用于通过其名称或ID引用bean。由于无法找到名称或ID为“ small”的xml条目,因此它尝试按类型进行匹配,并找到了两个Size实例。 以下将起作用: 虽然看起

    • 我正在尝试创建多个数据源。这就是我在配置类中的内容:如果我注释@primary注释,它将引发一个异常,并且应用程序没有启动,为什么@qualifier单独不能工作?为什么它坚持将其中一个数据源作为主要数据源?

    • 我有3个数据源,我设置如下: 我正在尝试将它们自动加入到我的消费者阶层中,如下所示:

    • 我在玩和 这是我的应用程序上下文文件 以下是我的Java类 HelloWorld1。JAVA 住址JAVA 这里是我尝试运行东西的地方-应用程序。JAVA 我一直在得到这个异常-理想情况下我不应该,因为我已经定义了id为'address1'的@Qualifier注释-所以它不应该抛出异常 警告:上下文初始化过程中遇到的异常-取消刷新尝试:org.springframework.beans.fact

    • 我在试着为Spock中的控制器写一个测试。 应用程序类只是Spring Boot最简单的配置(支持自动扫描)。它提供了一个with PasswordEncryptor。我想用提供模拟的bean替换应用程序中的这个bean。 没有注释,所以Spring不知道应该注入哪个bean。不幸的是,我不知道如何让Spring通过本地配置从应用程序中替换bean。

    • 当您创建多个相同类型的bean并且只想使用属性连接其中一个bean时,可能会出现这种情况。 在这种情况下,您可以使用@Qualifier注释和@Autowired通过指定要连接的确切bean来消除混淆。 以下是显示@Qualifier注释使用的示例。 例子 (Example) 让我们有一个可用的Eclipse IDE,并按照以下步骤创建一个Spring应用程序 - 脚步 描述 1 创建一个名为Sp