Spring框架中@Inject和@Autowired有什么区别?在什么条件下使用哪一个?

发布于 2024-11-30 11:43:28 字数 298 浏览 0 评论 0 原文

我正在浏览 SpringSource 上的一些博客,在其中一个博客中,作者正在使用 @Inject,我想他也可以使用 @Autowired

这是一段代码:

@Inject private CustomerOrderService customerOrderService;

那么使用 @Inject@Autowired 之间有什么区别,我将不胜感激如果有人解释了它们的区别以及在什么情况下使用哪一个。

I am going through some blogs on SpringSource and in one of the blogs, the author is using @Inject and I suppose he can also use @Autowired.

Here is the piece of code:

@Inject private CustomerOrderService customerOrderService;

So what is the difference between using @Inject and @Autowired and would appreciate it if someone explained their difference and which one to use under what situation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

深海不蓝 2024-12-07 11:43:28

假设您在这里指的是 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.

樱娆 2024-12-07 11:43:28

这是一篇博客文章 比较了 @Resource@Inject@Autowired,并且看起来做得相当全面。

从链接:

测试 2 和测试 2 除外7 配置和结果
完全相同的。当我仔细查看引擎盖下时,我确定
“@Autowired”和“@Inject”注释的行为相同。两者都
这些注释使用“AutowiredAnnotationBeanPostProcessor”来
注入依赖项。可以使用“@Autowired”和“@Inject”
可互换以注入 Spring bean。然而“@Resource”
注释使用“CommonAnnotationBeanPostProcessor”来注入
依赖关系。即使他们使用不同的后处理器类
它们的行为几乎相同。下面是他们的总结
执行路径。

作者引用的测试 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:

With the exception of test 2 & 7 the configuration and outcomes were
identical. When I looked under the hood I determined that the
‘@Autowired’ and ‘@Inject’ annotation behave identically. Both of
these annotations use the ‘AutowiredAnnotationBeanPostProcessor’ to
inject dependencies. ‘@Autowired’ and ‘@Inject’ can be used
interchangeable to inject Spring beans. However the ‘@Resource’
annotation uses the ‘CommonAnnotationBeanPostProcessor’ to inject
dependencies. Even though they use different post processor classes
they all behave nearly identically. Below is a summary of their
execution paths.

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.

黒涩兲箜 2024-12-07 11:43:28

为了处理没有连接的情况,可以使用将 @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 to false.

But when using @Inject, the Provider interface works with the bean which means that the bean is not injected directly but with the Provider.

撩人痒 2024-12-07 11:43:28

关键区别(在阅读 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.

时光沙漏 2024-12-07 11:43:28

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

找回味觉 2024-12-07 11:43:28

最好一直使用@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.

笑,眼淚并存 2024-12-07 11:43:28

@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 the Provider 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 no required 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).

柠檬心 2024-12-07 11:43:28

除了上述之外:

  1. @Autowired bean 的默认范围是 Singleton,而使用 JSR 330 @Inject 注释就像 Spring 的原型。
  2. JSR 330 中没有使用 @Inject 的 @Lazy 等效项。
  3. JSR 330 中没有使用 @Inject 的 @Value 等效项。

In addition to the above:

  1. The default scope for @Autowired beans is Singleton whereas using JSR 330 @Inject annotation it is like Spring's prototype.
  2. There is no equivalent of @Lazy in JSR 330 using @Inject.
  3. There is no equivalent of @Value in JSR 330 using @Inject.
岁月打碎记忆 2024-12-07 11:43:28

@Inject 没有“required”属性

@Inject has no 'required' attribute

疏忽 2024-12-07 11:43:28

@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

旧街凉风 2024-12-07 11:43:28

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

心房的律动 2024-12-07 11:43:28

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文