注入和资源以及自动装配注释 [英] Inject and Resource and Autowired annotations

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

问题描述

@Inject@Resource@Autowired 注解有什么区别?

What's the difference between @Inject and @Resource and @Autowired annotations?

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

When should we use each of them?

推荐答案

@Inject vs. @Autowire vs. @Resource 的区别?

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

@Autowired: spring 专有注解(相对于@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: 基于 JSR-330(Java 依赖注入)的注解识别可注入的构造函数、方法和字段.这个注解几乎完全替代了 Spring 的 @Autowired 注解.因此,您可以选择使用 @Inject,而不是使用 Spring 特定的 @Autowired 注解.@Autowired 和 @Inject 之间的区别之一是 @Inject 没有必填字段,因此如果我们找不到合适的对象来注入它会失败,而 @Autowired 可以使用 required=false 并允许可以为空的字段(仅如果需要的话!).@Inject 注解的优点是,你可以让@Inject 注入一个Provider,而不是直接注入一个引用.Provider 接口可以实现 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 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: 基于 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天全站免登陆