4

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;

}
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Lenik
  • 13,946
  • 17
  • 75
  • 103

1 Answers1

7

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • @谢继雷'Lenik use `@Service` rather than `@Bean` – Bozho Mar 08 '11 at 13:39
  • @Bozho: `@Service` and `@Component` are `ElementType.TYPE` targeted, but I don't want to create extra types. So, I guess `@Bean` won't work, right? – Lenik Mar 08 '11 at 15:04
  • @谢继雷'Lenik no. you must have a `Book` class that is annotated and made spring-managed. – Bozho Mar 08 '11 at 15:09
  • I have used @Bean for @Configuration definition and it works fine, albeit haven't used it with qualifiers. – CarlosZ Mar 08 '11 at 15:12
  • 1
    @CarlosZ @谢继雷'Lenik well, using java-config (`@Configuration`) perhaps doesn't work with qualifiers. – Bozho Mar 08 '11 at 15:17