简体   繁体   English

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

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

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)我有@Configuration ,所有bean都具有相同的限定符(许多相同类型的对象共存于一个容器中,它们的依赖关系和对象本身都是合格的)

@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 (ie for the whole configuraion, without repeating the annotation several times)?有没有办法对给定配置中的所有 bean 和注入使用单个限定符(即,对于整个配置,无需多次重复注释)?

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.仅当您有多个相同类型的 bean 时才需要限定符,以允许 Spring 确定您要注入哪一个。 In your case foo, bar and baz all have different types在您的情况下,foo、bar 和 baz 都有不同的类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM