当我们可以为 bean 命名时,为什么还要使用限定符呢?
当同一类型(类)的不同 bean 可以有不同的名称时,为什么还要对 @Bean 使用限定符?
@Bean
@Qualifier("fooConfig")
public Baz method1() {
}
下面的代码是不是更简洁?
@Bean("fooConfig")
public Baz method1() {
}
如果我创建两个具有不同名称的相同类型的bean(使用@Bean注释),那么我们可以使用@Qualifier注释(可以添加在字段/构造函数参数/setter上)在另一个bean中专门注入它们吗?
@Bean("fooConfig")
public Baz method1(){
}
@Bean("barConfig")
public Baz method2(){
}
// constructor parameter of a different bean
final @Qualifier("fooConfig") Baz myConfig
如果上述情况成立,那么我们在哪里使用@Qualifier(与@Bean或@Component一起使用)而不是给bean命名,如下所示?
@Bean
@Qualifier("fooConfig")
public Baz method1(){
}
@Bean
@Qualifier("barConfig")
public Baz method2(){
}
// constructor parameter of a different bean
final @Qualifier("fooConfig") Baz myConfig
Why do we use qualifiers with @Bean when we can have different names for different beans of the same type (class)?
@Bean
@Qualifier("fooConfig")
public Baz method1() {
}
Isn't the following code more clean?
@Bean("fooConfig")
public Baz method1() {
}
If I create two beans of the same type with different names (using @Bean annotation), then can we inject them specifically using the @Qualifier annotation(can be added on field/constructor parameter/setter) in another bean?
@Bean("fooConfig")
public Baz method1(){
}
@Bean("barConfig")
public Baz method2(){
}
// constructor parameter of a different bean
final @Qualifier("fooConfig") Baz myConfig
If the above is true, then where do we use @Qualifier (with @Bean or @Component) instead of giving the bean a name as shown below?
@Bean
@Qualifier("fooConfig")
public Baz method1(){
}
@Bean
@Qualifier("barConfig")
public Baz method2(){
}
// constructor parameter of a different bean
final @Qualifier("fooConfig") Baz myConfig
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我非常喜欢另一种工作方式。当然,如果您为豆提供一个唯一的名称,那就是您所需要的?
给定下面的示例,很容易看出,春季将根据用于创建bean的方法名称命名bean。换句话说,如果您给豆类明智的名称,则代码应该变得不言自明。将豆类注入其他类时,这也有效。
最终结果是:
让我们不要过度完整的春天。
I quite like a different way of working. Surely if you provide a unique name for your bean, then that is all you need?
Given the example below, its easy to see that Spring will name the beans based on the method name used to create the beans. In other words, if you give your beans sensible names, then the code should become self-explanatory. This also works when injecting beans into other classes.
The end result of this is:
Lets not over-complicate Spring.
例如:
如果您尝试在某处注入 myComponent,Spring 足够聪明,可以找到上面的 bean。因为只有一个返回类型为MyCustomComponent的Bean。但如果有几个方法会返回 MyCustomComponent,那么您必须告诉 Spring 使用 @Qualifier 注解注入哪一个。
旁注:默认情况下,@Bean 注解 Spring 使用方法名称作为 bean 名称。您还可以指定其他名称,例如 @Bean("otherComponent")。
这是您的界面:
这是您的实现 1:
您的实现 2:
现在您正在注入它:
问题! Spring 应该如何知道要注入哪个类?测试1还是测试2?这就是为什么你用 @Qualifier 告诉它哪个类。
For example:
If you will try to inject myComponent somewhere, Spring is smart enough to find the bean above. Becaude there is only one Bean with return type MyCustomComponent. But if there was a couple of methods, that would return MyCustomComponent, then you would have to tell Spring which one to inject with @Qualifier annotation.
SIDENOTE: @Bean annotation by default Spring uses the method name as a bean name. You can also assign other name like @Bean("otherComponent").
This is you interface:
This is your implementation 1:
Your implementation 2:
Now you are injecting it like:
QUESTION! How is Spring supposed to know which class to inject? Test1 or Test2? That's why you tell it with @Qualifier which class.
我更喜欢不同的方法
不
使用@Qualifier因此,我们可以为每个接口定义单一职责,这种隔离对于每个初级人员来说都更具可读性。
I Prefer different method to
not
using @Qualifierso, we can defined the single responsibility to each interface and This kind of segregation is more readable to every junior.