英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论