哪个注释,@Resource (jsr250)或@Autowired (spring特定)我应该在DI中使用?

我已经成功地使用在过去,@资源(名称="blah")和@Autowired @Qualifier("blah")

我的直觉是坚持使用@Resource标签,因为它已经被jsr的人批准了。 有人对此有强烈的想法吗?


当前回答

这是我从Spring 3.0中得到的。x参考手册:-

Tip 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. As a specific consequence of this semantic difference, beans that are themselves defined as a collection or map type cannot be injected through @Autowired, because type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection or map bean by unique name. @Autowired applies to fields, constructors, and multi-argument methods, allowing for narrowing through qualifier annotations at the parameter level. By contrast, @Resource is supported only for fields and bean property setter methods with a single argument. As a consequence, stick with qualifiers if your injection target is a constructor or a multi-argument method.

其他回答

这是我从Spring 3.0中得到的。x参考手册:-

Tip 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. As a specific consequence of this semantic difference, beans that are themselves defined as a collection or map type cannot be injected through @Autowired, because type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection or map bean by unique name. @Autowired applies to fields, constructors, and multi-argument methods, allowing for narrowing through qualifier annotations at the parameter level. By contrast, @Resource is supported only for fields and bean property setter methods with a single argument. As a consequence, stick with qualifiers if your injection target is a constructor or a multi-argument method.

这里有一个注释: SpringBeanAutowiringSupport。processInjectionBasedOnCurrentContext和SpringBeanAutowiringSupport。processInjectionBasedOnServletContext不能使用@Resource注释。所以,这是有区别的。

@Autowired(或@Inject)和@Resource都能很好地工作。但在概念上或意义上存在差异

@Resource意思是让我知道一个已知的资源的名称。名称从带注释的setter或字段的名称中提取,或者从name- parameter中提取。 @Inject或@Autowired尝试按类型插入合适的其他组件。

基本上这是两个截然不同的概念。不幸的是,@Resource的spring实现有一个内置的回退,当名称解析失败时就会启动。在本例中,它返回到按类型的@Autowired-kind解析。虽然这种回退很方便,但恕我说,它会引起很多混乱,因为人们没有意识到概念上的差异,并且倾向于使用@Resource进行基于类型的自动装配。

在3.0之前的春季版本中,哪种都不重要。

spring 3.0支持标准(JSR-330)注释@javax.inject。Inject -使用它,使用@Qualifier的组合。注意,spring现在也支持@javax.inject。限定符元注释:

@Qualifier
@Retention(RUNTIME)
public @interface YourQualifier {}

所以你可以有

<bean class="com.pkg.SomeBean">
   <qualifier type="YourQualifier"/>
</bean>

or

@YourQualifier
@Component
public class SomeBean implements Foo { .. }

然后:

@Inject @YourQualifier private Foo foo;

这减少了字符串名称的使用,字符串名称可能会拼写错误,并且更难维护。


至于最初的问题:两者都没有指定注释的任何属性,而是按类型执行注入。区别在于:

@Resource允许您指定注入bean的名称 @Autowired允许您将其标记为非强制的。

当您从这两个注释的基类进行严格分析时。您将认识到以下不同之处。

@Autowired使用AutowiredAnnotationBeanPostProcessor注入依赖项。 @Resource使用CommonAnnotationBeanPostProcessor注入依赖项。

尽管它们使用不同的后置处理器类,但它们的行为几乎相同。 关键的区别在于它们的执行路径,我在下面强调了这一点。

@Autowired / @Inject

1.按类型匹配 2.限定词限制 3.名称匹配

@Resource

1.名称匹配 2.按类型匹配 3.通过限定符进行限制(如果通过名称找到匹配则忽略)