如何为Spring配置中的所有bean设置一个共同的@Qualifier?

huangapple go评论72阅读模式
英文:

How to set a common @Qualifier for all beans inside Spring's Configuration?

问题

我有一个带有相同限定符的@Configuration(相同类型的许多对象在一个容器内共存,它们的依赖关系和对象本身都有限定符)

@Configuration
class ExampleConfiguration {
    @Bean @Qualifier("foo") Foo foo() { ... }
    @Bean @Qualifier("foo") Bar bar(@Qualifier("foo") Foo foo) { ... }
    @Bean @Qualifier("foo") Baz baz(@Qualifier("foo") Bar foo) { ... }
}

是否有一种方法可以在给定配置内为所有bean和注入使用单个限定符(即为整个配置,而不重复注解多次)?

英文:

I have @Configuration with all beans having the same qualifier (many objects of the identical types coexist within one container, their dependencies and the objects themselves are qualified)

@Configration
class ExampleConfiguration {
    @Bean @Qualifier("foo") Foo foo() { ... }
    @Bean @Qualifier("foo") Bar bar(@Qualifier("foo") Foo foo) { ... }
    @Bean @Qualifier("foo") Baz baz(@Qualifier("foo") Bar foo) { ... }
}

Is there a way to use a single qualifier for all beans and injections inside given configuration (i.e for the whole configuraion, without repeating the annotation several times)?

答案1

得分: 3

什么是您想要通过限定符实现的目标?

只有在您有多个相同类型的bean需要让Spring确定要注入哪一个时,才需要限定符。在您的情况下,foo、bar和baz都具有不同的类型。

英文:

What are you trying to achieve with the qualifiers?

A qualifier is only needed when you have multiple beans of the same type to allow Spring to determine which one you want to inject. In your case foo, bar and baz all have different types

huangapple
  • 本文由 发表于 2020年8月6日 20:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63283243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定