spring 注入和资源以及自动装配的注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20450902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:23:28  来源:igfitidea点击:

Inject and Resource and Autowired annotations

springdependency-injectionannotationscdiautowired

提问by oxygenan

What's the difference between @Injectand @Resourceand @Autowiredannotations?

@Injectand@Resource@Autowiredannotations和有什么不一样?

When should we use each of them?

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

回答by Haim Raman

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

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

@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.

@Autowired:spring 专有注解(与@Inject 和@Resource 相对),按类型注入资源,即通过注解字段或承包商的接口的类。如果我们几乎没有接口或子类的实现,我们可以使用 @Qualifier 批注缩小选择范围以避免歧义。对于回退匹配,bean 名称被视为默认限定符值。尽管您可以使用此约定按名称引用特定的 bean,但 @Autowired 从根本上讲是关于带有可选语义限定符的类型驱动注入。

@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

@注入:基于 JSR-330(Java 依赖注入)的注解标识了可注入的构造函数、方法和字段。这个注解几乎完全替代了 Spring 的 @Autowired 注解。因此,您可以选择使用 @Inject,而不是使用特定于 Spring 的 @Autowired 注释。@Autowired 和 @Inject 之间的区别之一是 @Inject 没有 required 字段,所以如果我们找不到合适的对象来注入它将会失败,而 @Autowired 可以使用 required=false 并允许 nullable 字段(仅如果需要的话!)。@Inject 注解的优点是可以让 @Inject 注入一个 Provider,而不是直接注入引用。Provider 接口支持延迟注入 bean 引用和注入多个 bean 实例。如果我们几乎没有接口或子类的实现,我们可以使用 @Named 注释缩小选择范围以避免歧义。@Named 注释的工作方式很像 Spring 的 @Qualifier

@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.

@Resource:基于 JSR-250 的注解。@Resource 与 @Autowired 和 @Inject 非常相似,但主要区别在于找出要注入的所需 bean 所采用的执行路径。@Resource 将首先按名称、然后按类型、最后按限定符缩小搜索范围(如果按名称找到匹配则忽略)。@Autowired 和 @Inject 将首先按类型缩小搜索范围,然后是限定符,最后是名称。

回答by eztam

|------------|---------------|---------------|---------------|-----------------------|
|            | Setter/Field  | Constructor   | Applicable to | Matching order        |
|            | injection     | injection     | type          |                       |
|------------|---------------|---------------|---------------|-----------------------|
| @Autowired |       X       |       X       |               | Type, Qualifier, Name |
|------------|---------------|---------------|---------------|-----------------------|
| @Inject    |       X       |       X       |               | Type, Qualifier, Name |
|------------|---------------|---------------|---------------|-----------------------|
| @Resource  |       X       |               |       X       | Name, Type, Qualifier |
|------------|---------------|---------------|---------------|-----------------------|

So in Spring dependency injection @Injectand @Autowiredhave exactly the same behaviour.

所以在 Spring 依赖注入中@Inject@Autowired具有完全相同的行为。

回答by Andriy Kryvtsun

In addition to @Haim answer there is good descriptionof the difference between Spring and JSR-330 (Dependency Injection for Java) annotations and how to use the last with Spring.

除了@Haim 的回答之外,还有关于Spring 和 JSR-330(Java 依赖注入)注释之间的区别以及如何在 Spring 中使用最后一个注释的很好的描述