Spring框架中@Inject和@Autowired有什么区别?在什么条件下使用哪一个?
我正在浏览 SpringSource 上的一些博客,在其中一个博客中,作者正在使用 @Inject
,我想他也可以使用 @Autowired
。
这是一段代码:
@Inject private CustomerOrderService customerOrderService;
那么使用 @Inject
和 @Autowired
之间有什么区别,我将不胜感激如果有人解释了它们的区别以及在什么情况下使用哪一个。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
假设您在这里指的是
javax.inject。注入
注释。@Inject
是 Java CDI 的一部分 (上下文和依赖注入)Java EE 6 (JSR-299) 中引入的标准,了解更多。 Spring 选择支持使用与自己的@Autowired
注释同义的@Inject
注释。所以,回答你的问题,
@Autowired
是Spring自己的注释。@Inject
是称为 CDI 的 Java 技术的一部分,它定义了类似于 Spring 的依赖注入标准。在 Spring 应用程序中,这两个注释的工作方式相同,因为 Spring 决定除了它们自己的注释之外还支持一些 JSR-299 注释。Assuming here you're referring to the
javax.inject.Inject
annotation.@Inject
is part of the Java CDI (Contexts and Dependency Injection) standard introduced in Java EE 6 (JSR-299), read more. Spring has chosen to support using the@Inject
annotation synonymously with their own@Autowired
annotation.So, to answer your question,
@Autowired
is Spring's own annotation.@Inject
is part of a Java technology called CDI that defines a standard for dependency injection similar to Spring. In a Spring application, the two annotations work the same way as Spring has decided to support some JSR-299 annotations in addition to their own.这是一篇博客文章 比较了
@Resource
、@Inject
和@Autowired
,并且看起来做得相当全面。从链接:
作者引用的测试 2 和 7 分别是“通过字段名称注入”和“尝试使用错误的限定符解析 bean”。
结论应该为您提供所需的所有信息。
Here is a blog post that compares
@Resource
,@Inject
, and@Autowired
, and appears to do a pretty comprehensive job.From the link:
Tests 2 and 7 that the author references are 'injection by field name' and 'an attempt at resolving a bean using a bad qualifier', respectively.
The Conclusion should give you all the information you need.
为了处理没有连接的情况,可以使用将
@Autowired
required
属性设置为false
的 bean。但是当使用@Inject时,Provider接口与bean一起工作,这意味着bean不是直接注入的,而是通过Provider注入的。
To handle the situation in which there is no wiring, beans are available with
@Autowired
required
attribute set tofalse
.But when using
@Inject
, the Provider interface works with the bean which means that the bean is not injected directly but with the Provider.关键区别(在阅读 Spring Docs) 是,
@Autowired
具有 'required ' 属性同时@Inject 没有“必需”属性。The key difference(noticed when reading the Spring Docs) between
@Autowired
and@Inject
is that,@Autowired
has the 'required' attribute while the @Inject has no 'required' attribute.从 Spring 3.0 开始,Spring 支持 JSR-330 依赖注入注释(
@Inject
、@Named
、@Singleton
)。Spring中有一个单独的部分关于它们的文档,包括与 Spring 等效项的比较。
As of Spring 3.0, Spring offers support for JSR-330 dependency injection annotations (
@Inject
,@Named
,@Singleton
).There is a separate section in the Spring documentation about them, including comparisons to their Spring equivalents.
最好一直使用@Inject。因为它是java配置方法(由sun提供),这使得我们的应用程序与框架无关。因此,如果您也参加了春季课程,那么您的课程就会有效。
如果您使用@Autowired,它将仅适用于spring,因为@Autowired是spring提供的注释。
Better use @Inject all the time. Because it is java configuration approach(provided by sun) which makes our application agnostic to the framework. So if you spring also your classes will work.
If you use @Autowired it will works only with spring because @Autowired is spring provided annotation.
@Autowired
注解是在 Spring 框架中定义的。@Inject
注解是一个标准注解,其定义在标准 中“Java 依赖注入”(JSR-330)。 Spring(从 3.0 版开始)支持标准 JSR-330 中定义的通用依赖注入模型。 (Google Guice 框架 和 Picocontainer框架也支持这种模型)。使用@Inject可以注入对Provider接口实现的引用,这允许注入延迟引用。
注释
@Inject
和@Autowired
- 几乎是完全的类比。与@Autowired 注解一样,@Inject 注解也可用于自动绑定属性、方法和构造函数。与
@Autowired
注释相反,@Inject
注释没有required
属性。因此,如果找不到依赖项 - 将引发异常。绑定属性的说明也存在差异。如果在选择注入组件时存在歧义,则应添加
@Named
限定符。在类似的情况下,@Autowired
注释将添加@Qualifier
限定符(JSR-330 定义了它自己的@Qualifier
注释,并通过此限定符注释@Named
已定义)。@Autowired
annotation is defined in the Spring framework.@Inject
annotation is a standard annotation, which is defined in the standard "Dependency Injection for Java" (JSR-330). Spring (since the version 3.0) supports the generalized model of dependency injection which is defined in the standard JSR-330. (Google Guice frameworks and Picocontainer framework also support this model).With
@Inject
can be injected the reference to the implementation of theProvider
interface, which allows injecting the deferred references.Annotations
@Inject
and@Autowired
- is almost complete analogies. As well as@Autowired
annotation,@Inject
annotation can be used for automatic binding properties, methods, and constructors.In contrast to
@Autowired
annotation,@Inject
annotation has norequired
attribute. Therefore, if the dependencies will not be found - will be thrown an exception.There are also differences in the clarifications of the binding properties. If there is ambiguity in the choice of components for the injection the
@Named
qualifier should be added. In a similar situation for@Autowired
annotation will be added@Qualifier
qualifier (JSR-330 defines it's own@Qualifier
annotation and via this qualifier annotation@Named
is defined).除了上述之外:
@Inject
的 @Lazy 等效项。@Inject
的 @Value 等效项。In addition to the above:
@Autowired
beans is Singleton whereas using JSR 330@Inject
annotation it is like Spring's prototype.@Inject
.@Inject
.@Inject
没有“required”属性@Inject
has no 'required' attribute@Autowired(必需= false)
默认情况下,必须满足 @Autowired 的依赖注入,因为 required 属性的值默认为 true。我们可以通过使用 @Autowired(required=false) 来更改此行为。在这种情况下,如果没有找到用于依赖注入的 bean,则不会出现错误。
请看一下
https://www.concretepage.com/spring/spring-autowired-注解#required-false
但是@Inject不需要(required = false),如果没有找到依赖项,它不会出现错误
@Autowired(required=false)
By default the dependency injection for @Autowired must be fulfilled because the value of required attribute is true by default. We can change this behavior by using @Autowired(required=false). In this case if bean is not found for dependency injection, it will not through error.
Please have look at
https://www.concretepage.com/spring/spring-autowired-annotation#required-false
But @Inject doesn’t need (required=false) it will not through error if dependency is not found
@Inject
注释是 JSR-330 注释集合之一。这具有按类型匹配、按限定符匹配、按名称匹配执行路径。这些执行路径对于 setter 和字段注入都有效。
@Autowired
注解的行为与@Inject
注解相同。唯一的区别是@Autowired 注解是Spring 框架的一部分。@Autowired
注解也有上述执行路径。因此,我推荐使用@Autowired
作为您的答案。The
@Inject
annotation is one of the JSR-330 annotations collection. This has Match by Type,Match by Qualifier, Match by Name execution paths.These execution paths are valid for both setter and field injection.The behavior of
@Autowired
annotation is same as the@Inject
annotation. The only difference is the@Autowired
annotation is a part of the Spring framework.@Autowired
annotation also has the above execution paths. So I recommend the@Autowired
for your answer.Inject 和 Autowired 都提供相同的功能,即注入 bean。
@Inject - 它在 javax.inject 包中定义,是 Java 的一部分。
如果您的应用程序不是 spring 框架,则使用 Inject 注释,因为它是 java 的一部分。
@Autowired – 它在 org.springframework.bean.factory 包中定义,是 Spring 框架的一部分。
如果您的应用程序是基于 Spring 的应用程序,那么最好使用 Autowired 注释。
Inject and Autowired both serves the same functionality i.e to inject the bean.
@Inject - It's defined in the javax.inject package and it's part of Java.
If your application is not spring framework then use Inject annotation as it's part of java.
@Autowired – It's defined in the package org.springframework.bean.factory and part of Spring framework.
If your application is Spring based application then better use Autowired annotation.