[英]Qualifier not working in spring
CallingApp.java 调用App.java
@Service
@ComponentScan(basePackages = { "com.codegeekslab.type" })
public class CallingApp {
@Autowired
@Qualifier("BasicPhone")
private Phone phone;
public CallingApp(Phone phone) {
this.phone = phone;
}
public void makeCall(int number) {
phone.openApp(number);
}
}
Phone.java 电话.java
package com.geekslab.device;
public interface Phone {
public void openApp(int number);
}
BasicPhone.java BasicPhone.java
package com.codegeekslab.type;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.geekslab.device.Phone;
@Component("BasicPhone")
public class BasicPhone implements Phone {
{
System.out.println("BasicPhone");
}
public void openApp(int number) {
System.out.println("calling via simcard... " + number);
}
}
SmartPhone.java SmartPhone.java
package com.codegeekslab.type;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.geekslab.device.Phone;
@Component("SmartPhone")
public class SmartPhone implements Phone {
{
System.out.println("SmartPhone");
}
public void openApp(int number) {
System.out.println("calling via whatsapp..." + number);
}
}
Test.java Test.java
package com.codegeekslab.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.codegeekslab.app.CallingApp;
import com.codegeekslab.type.BasicPhone;
import com.codegeekslab.type.SmartPhone;
import com.geekslab.device.Phone;
public class Test {
public static void main(String[] args) {
//ApplicationContext context =
// new GenericXmlApplicationContext("beans.xml");
//SpringHelloWorld helloSpring = context.getBean("springHelloWorld", SpringHelloWorld.class);
//comment this for xml less spring
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.codegeekslab.app","com.codegeekslab.type");
//context.register( BasicPhone.class,SmartPhone.class,CallingApp.class);
context.refresh();
CallingApp callingApp = context.getBean("callingApp", CallingApp.class);
callingApp.makeCall(99999);
}
}
Even though i am giving qualifier as @Qualifier("BasicPhone")
in CallingApp
class ,I am getting Exception as follows: 即使我在
CallingApp
类@Qualifier("BasicPhone")
限定符指定为@Qualifier("BasicPhone")
,但CallingApp
收到异常 ,如下所示:
No qualifying bean of type [com.geekslab.device.Phone] is defined: expected single matching bean but found 2: BasicPhone,SmartPhone
没有定义类型为[com.geekslab.device.Phone]的合格Bean:预期的单个匹配Bean,但找到2:BasicPhone,SmartPhone
You pass phone as a constructor argument in your CallingApp service without specifying the bean. 您可以在CallingApp服务中将phone作为构造函数参数传递而无需指定Bean。
Try either to put a qualifier at your constructor or stick to the autowire injection which is something your already do. 尝试在构造函数中放置一个限定符,或者坚持自动布线注入,这已经是您要做的了。
i removed the CallingApp class constructor and it worked. 我删除了CallingApp类的构造函数,它起作用了。
public CallingApp(Phone phone) {
this.phone = phone;
}
As Constructor was overriding the setter method. 由于构造方法覆盖了setter方法。
You need to add no argument constructor 您无需添加任何参数构造函数
public CallingApp(){
//do nothing
}
public CallingApp(Phone phone) {
this.phone = phone;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.