在Spring中,有两个单独的注释用于按名称执行依赖注入,javax.annotation.Resource和javax.inject.Named. documentation at Spring表示@Resource应该按名称用于注入:
If you intend to express annotation-driven injection by name,do not
primarily use @Autowired,even if is technically capable of referring
to a bean name through @Qualifier values. Instead,use the JSR-250
@Resource annotation,which is semantically defined to identify a
specific target component by its unique name,with the declared type
being irrelevant for the matching process.
上面的内容有点令人困惑,因为Spring只提倡@Resource而不是@Autowired和@Qualifer.直到later in the documentation才提及@Named.
JSR-250定义了@Resource,而JSR-330定义了@Inject和@Named.我知道它们可以很容易地在Spring中混合和匹配.使用哪个JSR?
看起来像Guice和CDI的可移植性会很好,因此使用JSR-330注释.另一方面,文档还指出Spring中使用JSR-330注释时的几个limitations.
注释注释的最佳实践(如果有)是什么?
谢谢.
使用Spring时,我更倾向于选择一个而不是另一个,而是向后兼容Spring 2.5以及是否可以在类路径中添加/假设javax.inject.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。