Java Spring@Autowired和@Qualifier
是否通过@Autowired自动检测到?当使用@Qualifier时,是否通过名称进行依赖项注入?我们如何使用这些注释进行setter和constructor注入?您可以使用Java Spring@Autowired和@Qualifier,java,spring,Java,Spring,是否通过@Autowired自动检测到?当使用@Qualifier时,是否通过名称进行依赖项注入?我们如何使用这些注释进行setter和constructor注入?您可以使用@Qualifier以及@Autowired。事实上,如果发现不明确的bean类型,spring将要求您显式地选择bean,在这种情况下,您应该提供限定符 @Component @Qualifier("staff") public Staff implements Person {} @Component @Qualif
@Qualifier
以及@Autowired
。事实上,如果发现不明确的bean类型,spring将要求您显式地选择bean,在这种情况下,您应该提供限定符
@Component
@Qualifier("staff")
public Staff implements Person {}
@Component
@Qualifier("employee")
public Manager implements Person {}
@Component
public Payroll {
private Person person;
@Autowired
public Payroll(@Qualifier("employee") Person person){
this.person = person;
}
}
例如,在以下情况下,有必要提供一个限定符
@Component
@Qualifier("staff")
public Staff implements Person {}
@Component
@Qualifier("employee")
public Manager implements Person {}
@Component
public Payroll {
private Person person;
@Autowired
public Payroll(@Qualifier("employee") Person person){
this.person = person;
}
}
编辑:
在Lombok 1.18.4中,当您使用@Qualifier时,最终可以避免构造函数注入的样板文件,因此现在可以执行以下操作:
@Component
@Qualifier("staff")
public Staff implements Person {}
@Component
@Qualifier("employee")
public Manager implements Person {}
@Component
@RequiredArgsConstructor
public Payroll {
@Qualifier("employee") private final Person person;
}
假设您正在使用新的lombok.config规则copyableAnnotations(通过将以下内容放置在项目根目录的lombok.config中):
这是最近在最新的lombok 1.18.4中引入的
- 详细讨论问题的地方
- github上的
- 并计划将其付诸实施
public Payroll {
private final Person person;
@Autowired
@Qualifier("employee")
public void setPerson(Person person) {
this.person = person;
}
}
如果您使用的是构造函数注入,那么注释应该放在构造函数上,否则代码将无法工作。像下面那样使用它-
public Payroll {
private Person person;
@Autowired
public Payroll(@Qualifier("employee") Person person){
this.person = person;
}
}
当存在多个相同类型的bean时,
@Qualifier
注释用于解决自动连接冲突
@Qualifier
注释可用于用@Component
注释的任何类或用@Bean
注释的方法。此注释也可以应用于构造函数参数或方法参数
例如:
public interface Vehicle {
public void start();
public void stop();
}
有两个bean,汽车和自行车实现汽车接口
@Component(value="car")
public class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car started");
}
@Override
public void stop() {
System.out.println("Car stopped");
}
}
@Component(value="bike")
public class Bike implements Vehicle {
@Override
public void start() {
System.out.println("Bike started");
}
@Override
public void stop() {
System.out.println("Bike stopped");
}
}
使用带有@限定符注释的@Autowired
在车辆服务中注入自行车bean。如果您没有使用@限定符
,它将抛出名词性和定义异常
@Component
public class VehicleService {
@Autowired
@Qualifier("bike")
private Vehicle vehicle;
public void service() {
vehicle.start();
vehicle.stop();
}
}
参考:-@Autowired
按类型自动连线(或搜索)
@限定符
按名称自动连线(或搜索)
@Qualifier
的其他备选选项是@Primary
@Component
@Qualifier("beanname")
public class A{}
public class B{
//Constructor
@Autowired
public B(@Qualifier("beanname")A a){...} // you need to add @autowire also
//property
@Autowired
@Qualifier("beanname")
private A a;
}
更多关于
@Component
@Qualifier("beanname")
public class A{}
public class B{
//Constructor
@Autowired
public B(@Qualifier("beanname")A a){...} // you need to add @autowire also
//property
@Autowired
@Qualifier("beanname")
private A a;
}
//If you don't want to add the two annotations, we can use @Resource
public class B{
//property
@Resource(name="beanname")
private A a;
//Importing properties is very similar
@Value("${property.name}") //@Value know how to interpret ${}
private String name;
}