问题
Spring has its own Qualifier
annotation, I think it's equivalent to the javax.inject.Named
annotation, which in turn is a concrete qualifier in JSR-330.
So, I'm wondering which version of Spring, if any, supports Qualifier?
Here is my example usage, unfortunately it doesn't work with spring-context 3.0.5:
@Retention(RUNTIME)
@javax.inject.Qualifier
public @interface Version {
String value();
}
@Configuration
public class MyConfig {
@Bean("book-12") @Version("a") Book book12a() { ... }
@Bean("book-12") @Version("b") Book book12b() { ... }
}
@Component
public class UserClass {
@Inject @Named("book-12") Book anybook12;
@Inject @Named("book-12") @Version("b") Book book12_b;
}
回答1:
Yes, it supports all javax.inject.*
annotations. I myself have used the javax.inject.Qualifier
Btw, I assume you want @Service
or @Component
instead of @Bean
, and you need your Book
class to be made spring-managed.
来源:https://stackoverflow.com/questions/5232353/does-spring-context-support-jsr-330-qualifier-on-bean-instances