[英]How to use @autowired in different classes that implement the same interface with only annotations
As I can choose which class to call only with annotations? 因为我可以选择仅使用注释来调用哪个类? For example i need to call my class "Test_2"
例如,我需要将我的班级命名为“ Test_2”
Interface: 接口:
public interface Inter {
public void useInterface();
}
Class 1: 第1类:
public class Test_1 implements Inter {
public void useInterface(){
System.out.println("Instance Class Test_1");
}
}
Class 2: 第2类:
public class Test_2 implements Inter {
public void useInterface(){
System.out.println("Instance Class Test_2");
}
}
Class Call: 课堂电话:
public class Call {
@Autowired
private Inter inter;
public Call(){
inter.useInterface();
}
}
anotate your classes this way: 用这种方式注释您的班级:
@Component("test1")
public class Test_1 implements Inter {
public void useInterface(){
System.out.println("Instance Class Test_1");
}
}
and use 和使用
@Autowired
@Qualifier("test1")
private Inter inter;
You need to use qualifier, ie: 您需要使用限定符,即:
@Autowired
@Qualifier("test_2")
private Inter inter;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.