注入和资源与自动装配注解 [英] Inject and Resource and Autowired annotations

查看:185
本文介绍了注入和资源与自动装配注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我 @Inject @Resource @Autowired 注释吗?

当我们应该使用他们每个人的?

When should we use each of them?

推荐答案

之间的差异与@注入与@Autowire @Resource?

The difference between @Inject vs. @Autowire vs. @Resource?

@Autowired:春季恰当的注释(相对于@Inject和@Resource),通过注解的字段或承包商的接口通过注入型,即由资源类的。在情况下,我们很少有实现的接口或子类,我们可以使用@Qualifier注解,以避免歧义缩小选择范围。对于一个备用的比赛,该bean的名称被认为是默认值预选赛。虽然你可以使用这个公约是指由特定的名称豆类,@Autowired从根本上说具有可选的语​​义预选赛型驱动的注入。

@Autowired: spring propriety annotation (as opposed to @Inject and @Resource) that inject a resource by-type, i.e. by the class of by the interface of the annotated field or contractor. In case we have few implementation of an interface or a subclass we can narrow down the selection using the @Qualifier annotation to avoid ambiguity. For a fallback match, the bean name is considered a default qualifier value. Although you can use this convention to refer to specific beans by name, @Autowired is fundamentally about type-driven injection with optional semantic qualifiers.

@Inject 基于JSR-330(Java依赖注入)标注标识注入的构造,方法和字段。这个注释是几乎完全下拉更换为Spring的@Autowired注解。因此,而不是使用特定Spring的@Autowired注解,你可以选择使用@Inject。其中一个@Autowired和@Inject之间的差异是@Inject不具备必要的字段,以便在情况下,我们无法找到一个合适的对象来注入会失败,而@Autowired可以将旧所需=虚假和无效允许能够字段(仅适用如果需要!)。
@Inject注解的优点是,而不是直接注入一个参考,你可以要求@Inject注入提供商。提供程序接口实现,除其他事项外,bean引用的懒惰注入和一个bean的多个实例的注入。
在情况下,我们很少有实现的接口或子类,我们可以使用@Named标注,以避免歧义缩小选择范围。 @Named标注很像Spring的@Qualifier

@Inject: Annotation based on JSR-330 (Dependency Injection for Java) identifies injectable constructors, methods, and fields. This annotation is an almost complete drop-in replacement for Spring’s @Autowired annotation. So, instead of using the Spring-specific @Autowired annotation, you might choose to use @Inject. One of the differences between @Autowired and @Inject is that @Inject does not have the required field so in case we fail to find a suitable object to injected it will fail while @Autowired can used required=false and allow null able field (only if required!). Advantage of @Inject annotation is that rather than inject a reference directly, you could ask @Inject to inject a Provider. The Provider interface enables, among other things, lazy injection of bean references and injection of multiple instances of a bean. In case we have few implementation of an interface or a subclass we can narrow down the selection using the @Named annotation to avoid ambiguity. @Named annotation works much like Spring’s @Qualifier

@Resource 基于JSR-250的注释。 @Resource颇为相似@Autowired和@Inject,但主要区别在于找出所需的bean注入所采取的执行路径。 @Resource会先缩小搜索按名称,然后按类型和最后通过预选赛(忽略,如果匹配的名字找到)。 @Autowired和@Inject会先缩小搜索范围按类型然后通过预选赛最后由名。

@Resource: annotation based on JSR-250. @Resource is quite similar to @Autowired and @Inject, but the main difference is the execution paths taken to find out the required bean to inject. @Resource will narrow down the search first by name then by type and finally by Qualifiers (ignored if match is found by name). @Autowired and @Inject will narrow down the search first by type then by qualifier and finally by the name.

这篇关于注入和资源与自动装配注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆