Java Spring 中的 @Qualifier 注释不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20159220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:35:16  来源:igfitidea点击:

@Qualifier Annotation in Spring is not working

javaspringspring-mvc

提问by Dani Petrick

I have just learnt Spring Framework and have been using Spring 2.5 for this learning. I have created three beans with these classes

我刚刚学习了 Spring Framework,并且一直在使用 Spring 2.5 进行学习。我用这些类创建了三个 bean

Food.java

食品.java

package com.spring.danipetrick;

public interface Food {
    void ingredients(); 
}

NasiGoreng.java

炒饭

package com.spring.danipetrick;

public class NasiGoreng implements Food {

    public NasiGoreng() {

    }

    public void ingredients() {
        System.out.println("Rice, Coconut oil, Egg, Crackers");
    }

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

Rendang.java

人当网

package com.spring.danipetrick;

public class Rendang implements Food {
    public void ingredients() {
        System.out.println("Beef, Coconut milk, spices");
    }

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

PecintaKuliner.java

PecintaKuliner.java

package com.spring.danipetrick;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class PecintaKuliner {

    @Autowired
    @Qualifier("nasigoreng")
    private Food food;

    @Autowired
    public void setFood(Food food) {
        this.food = food;
    }

    public Food getFood() {
        return this.food;
    }

    public void sayMaknyus() {
        System.out.println(food.toString() + " memang maknyus...");
    }
}

Xml configuration, qualifier-test.xml

xml配置,qualifier-test.xml

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

  <context:annotation-config />

  <bean id="bondan" class="com.spring.danipetrick.PecintaKuliner">
  </bean>

  <bean id="rendang" class="com.spring.danipetrick.Rendang"/>

  <bean id="nasigoreng" class="com.spring.danipetrick.NasiGoreng" />

</beans>

Finally, class with main method is QualifierTestMain.java:

最后,带有 main 方法的类是 QualifierTestMain.java:

package com.spring.danipetrick;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class QualifierTestMain {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("/qualifier-test.xml"); 

        PencintaKuliner bondan = (PencintaKuliner)context.getBean("bondan");
        bondan.sayMaknyus();
    }
}

When I run this project, I have an error like this

当我运行这个项目时,我有这样的错误

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.spring.danipetrick.Food] is defined: expected single matching bean but found 2: [rendang, nasigoreng]

引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义类型 [com.spring.danipetrick.Food] 的唯一 bean:预期的单个匹配 bean,但发现 2:[rendang, nasigoreng]

Why @Qualifier annotation is not working in my case?

为什么@Qualifier 注释在我的情况下不起作用?

采纳答案by Sandhu Santhakumar

Try only removing @Autowiredfrom setFood()in PecintaKuliner

尝试仅仅去除@Autowired来自setFood()PecintaKuliner

like

喜欢

@Autowired
@Qualifier("nasigoreng")
private Food food;

public void setFood(Food food) {
    this.food = food;
}

回答by Sotirios Delimanolis

Both your method and field are annotated with @Autowired. As such, Spring will try to inject both. On one of the runs, it will try to inject

您的方法和字段都用@Autowired. 因此,Spring 将尝试注入两者。在其中一次运行中,它会尝试注入

@Autowired
@Qualifier("nasigoreng")
private Food food;

which will work because the injection target is qualified.

这将起作用,因为注入目标是合格的。

The method however

方法不过

@Autowired
public void setFood(Food food) {
    this.food = food;
}

does not qualify the injection parameter so Spring doesn't know which bean to inject.

不限定注入参数,因此 Spring 不知道要注入哪个 bean。

Change the above to

把上面的改成

@Autowired
public void setFood(@Qualifier("nasigoreng") Food food) {
    this.food = food;
}

But you should decide one or the other, field or setter injection, otherwise it is redundant and may cause errors.

但是你应该决定一个或另一个,字段或setter注入,否则它是多余的,可能会导致错误。

回答by Nawanshu

I tried with Spring 4.2.4. Problem resolved just by adding <context:annotation-config />in configuration file.

我尝试使用 Spring 4.2.4。只需添加<context:annotation-config />配置文件即可解决问题 。

回答by Sachitha Ilayperuma

Try removing you constructor from the NasiGoreng class. It worked for me.

尝试从 NasiGoreng 类中删除构造函数。它对我有用。