spring-context 是否支持 bean 实例上的 JSR-330 限定符?
Spring有自己的Qualifier
注释,我认为它相当于javax.inject.Named
注释,而后者又是JSR-330中的具体限定符。
所以,我想知道哪个版本的 Spring(如果有的话)支持 Qualifier?
这是我的示例用法,不幸的是它不适用于 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,它支持所有
javax.inject.*
注释。我自己已经使用了javax.inject.Qualifier
顺便说一句,我假设你想要
@Service
或@Component
而不是@Bean< /code>,并且您需要将
Book
类设置为 spring 管理的。Yes, it supports all
javax.inject.*
annotations. I myself have used thejavax.inject.Qualifier
Btw, I assume you want
@Service
or@Component
instead of@Bean
, and you need yourBook
class to be made spring-managed.