Spring @Autowired 和 @Qualifier [英] Spring @Autowired and @Qualifier
问题描述
是否使用 @Autowired
自动检测到?
使用@Qualifier
时是否按名称进行依赖注入?
我们如何使用这些注解进行 setter 和构造函数注入?
Is it auto detected with @Autowired
?
Is it dependency injection by name when @Qualifier
is used?
How can we do setter and constructor injection using these annotations?
推荐答案
您可以将 @Qualifier
与 @Autowired
一起使用.事实上,如果找到不明确的 bean 类型,spring 会要求您明确选择 bean,在这种情况下,您应该提供限定符
You can use @Qualifier
along with @Autowired
. In fact spring will ask you explicitly select the bean if ambiguous bean type are found, in which case you should provide the qualifier
例如在以下情况下,必须提供限定符
For Example in following case it is necessary provide a qualifier
@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 时,终于可以避免构造函数注入的样板,因此现在可以执行以下操作:
In Lombok 1.18.4 it is finally possible to avoid the boilerplate on constructor injection when you have @Qualifier, so now it is possible to do the following:
@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 中):
provided you are using the new lombok.config rule copyableAnnotations (by placing the following in lombok.config in the root of your project):
# Copy the Qualifier annotation from the instance variables to the constructor
# see https://github.com/rzwitserloot/lombok/issues/745
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier
这是最近在最新的 lombok 1.18.4 中引入的.
This was recently introduced in latest lombok 1.18.4.
- 博客文章,其中详细讨论了该问题
- github 上的原始问题
- 还有 一个小型 github 项目,看看它的实际效果
- The blog post where the issue is discussed in detail
- The original issue on github
- And a small github project to see it in action
注意
如果您使用字段或 setter 注入,则必须将 @Autowired 和 @Qualifier 放在字段或 setter 函数的顶部,如下所示(它们中的任何一个都可以使用)
If you are using field or setter injection then you have to place the @Autowired and @Qualifier on top of the field or setter function like below(any one of them will work)
public Payroll {
@Autowired @Qualifier("employee") private final Person person;
}
或
public Payroll {
private final Person person;
@Autowired
@Qualifier("employee")
public void setPerson(Person person) {
this.person = person;
}
}
如果您使用构造函数注入,那么注释应该放在构造函数上,否则代码将无法工作.像下面一样使用它 -
If you are using constructor injection then the annotations should be placed on constructor, else the code would not work. Use it like below -
public Payroll {
private Person person;
@Autowired
public Payroll(@Qualifier("employee") Person person){
this.person = person;
}
}
这篇关于Spring @Autowired 和 @Qualifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!