注入和资源和自动连线注释 [英] Inject and Resource and Autowired annotations

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

问题描述

@Inject @Resource @Autowired 注释?

我们什么时候应该使用它们?

When should we use each of them?

推荐答案

@Inject与@Autowire与@Resource之间的区别?

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

@Autowired:弹簧适当注释(而不是@Inject和@Resource),通过类型注入资源,即由注释字段或承包商的接口的类。如果我们几乎没有实现接口或子类,我们可以使用@Qualifier注释来缩小选择范围,以避免歧义。对于回退匹配,bean名称被认为是默认限定符值。虽然你可以使用这个惯例通过名字来引用特定的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: / strong>基于JSR-330(针对Java的依赖注入)的注释识别可注入的构造函数,方法和字段。此注释是Spring的@Autowired注释几乎完全可以替代。所以,不用使用Spring特定的@Autowired注释,你可以选择使用@Inject。 @Autowired和@Inject之间的一个区别是@Inject没有必需的字段,以防万一我们找不到一个合适的对象来注入它将失败,而@Autowired可以使用required = false并允许null的能力字段如果需要!)。
@Inject注释的优点是,而不是直接注入引用,您可以要求@Inject注入提供程序。提供程序接口允许懒惰地注入bean引用和注入一个bean的多个实例。
如果我们几乎没有实现接口或子类,我们可以使用@Named注释来缩小选择范围,以避免歧义。 @Named注释非常像基于JSR-250的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 inject 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:注释。 @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天全站免登陆