Hello folks, what is the difference between @Autowired and @Inject annotation in Spring is one of the frequently asked Spring questions on Java interviews? Since everybody is now moved or moving to annotation-driven and Java configuration in Spring, this question has become even more important for prospective candidates looking for a Java web development job using the Spring framework.
大家好,Spring中的@Autowired和@Inject注解有什么区别,这是Java面试中常见的Spring问题之一?由于现在每个人都在Spring中转移到注解驱动和Java配置,因此对于使用Spring框架寻找Java Web开发工作的潜在候选人来说,这个问题变得更加重要。
The @Autowired
annotation is used for auto-wiring in the Spring framework. If you don’t know, autowiring is a process on which the Spring framework figure out the dependencies of a Spring bean, instead of you, a developer, explicitly specifying them in the application context file. You can annotate fields and constructor using @Autowired to tell Spring framework to find dependencies for you.
@Autowired
注解用于 Spring 框架中的自动装配。如果你不知道,自动装配是一个过程,在这个过程中,Spring 框架找出 Spring Bean 的依赖关系,而不是你,开发人员,在应用程序上下文文件中明确指定它们。您可以使用@Autowired来注解字段和构造函数,以告诉 Spring 框架为您查找依赖项。
The @Inject
annotation also serves the same purpose, but the main difference between them is that @Inject is a standard annotation for dependency injection and @Autowired is spring specific.
@Inject
注解也有相同的目的,但它们之间的主要区别在于@Inject是依赖注入的标准注解,而@Autowired是特定于Spring的。
Since Spring is not the only framework that provides dependency injection, in the future if you change your container and moves to another DI framework like Google Guice, you need to reconfigure your application.
由于 Spring 不是唯一提供依赖注入的框架,将来如果您更改容器并迁移到另一个 DI 框架(如 Google Guice),则需要重新配置您的应用程序。
You can potentially avoid that development effort by using standard annotations specified by JSR-330 e.g. @Inject, @Named, @Qualifier, @Scope, and @Singleton.
通过使用 JSR-330 指定的标准注解(例如 @Inject、@Named、@Qualifier、@Scope和@Singleton),您可以避免这种开发工作。
A bean declared to be auto-wired using @Inject will work in both Google Guice and Spring framework, and potentially any other DI container which supports JSR-330 annotations.
声明为使用@Inject自动装配的 Bean 可以在 Google Guice 和 Spring 框架中工作,并且可能适用于任何其他支持 JSR-330 注解的 DI 容器。
Good knowledge of essential Spring annotations is very important for a hands-on developer, and if you think you don’t know most of them, I suggest you check out Spring Framework 5: Beginner to Guru, one of my favorite courses to start with Spring. It’s also covers Spring 5.0, the latest version of the Spring framework.
对于实际开发人员来说,熟悉基本的 Spring 注解非常重要,如果您认为自己不了解其中的大部分,我建议您查看 Spring Framework 5:Beginner to Guru,这是我最喜欢从 Spring 开始的课程之一。它还涵盖了Spring 5.0,这是Spring框架的最新版本。
What is difference between @Autowired vs @Inject Annotations?
@Autowired 与@Inject注解有什么区别?
If you have worked with Hibernate and JPA in past then JSR-330 annotation is nothing but like JPA annotations which standardize the Object-Relational mapping across the framework. When you use the JPA annotations like @Entity, your code will not only work on Hibernate but also on other ORM tools and frameworks like Eclipse TopLink.
如果您过去使用过Hibernate和JPA,那么JSR-330注解就像JPA注解一样,它标准化了整个框架的对象关系映射。当你使用像@Entity这样的JPA注解时,你的代码不仅可以在Hibernate上运行,还可以在其他ORM工具和框架(如Eclipse TopLink)上运行。
Btw, like all similar things in the world, even though both @Autowired and @Inject serve the same purpose there are a couple of differences between them, let’s examine them briefly
顺便说一句,就像世界上所有类似的事情一样,即使@Autowired和@Inject都有相同的目的,它们之间也有一些区别,让我们简要检查一下它们
1. The first and most important difference between @Autowired and @Inject annotation is that the @Inject annotation is only available from Spring 3.0 onwards, so if you want to use annotation-driven dependency injection in Spring 2.5 then you have to use the @Autowired annotation.
1. @Autowired 注解和@Inject注解之间的第一个也是最重要的区别是,@Inject注解仅从 Spring 3.0 开始可用,所以如果你想在 Spring 2.5 中使用注解驱动的依赖注入,那么你必须使用 @Autowired 注解。
2. The second difference between these two annotations is that, unlike Spring’s @Autowired, the @Inject does not require the ‘required’ attribute.
2. 这两个注解之间的第二个区别是,与 Spring 的@Autowired不同,@Inject不需要“required”属性。
3. The third most common difference between @Autowired and @Inject annotation is that the former is Spring specific while the latter is the standard for Dependency Injection, specified in JSR-330.
3. @Autowired 和@Inject注解之间的第三个最常见的区别是前者是 Spring 特有的,而后者是 JSR-330 中指定的依赖注入标准。
In general, I recommend the use of JSR 330 annotation for DI, the @Inject annotation is as capable as Spring’s @Autowired, and if you want you can also mix and match this with Spring’s @Value and @Lazy annotations.
一般来说,我建议使用 JSR 330 注解进行 DI,@Inject 注解的功能与 Spring 的 @Autowired 一样强大,如果需要,您也可以将其与 Spring 的 @Value 和 @Lazy 注解混合搭配。
4. The @Autowired annotation was added on Spring 2.5 and used for annotation-driven dependency injection. It works in conjunction with @Component annotation and <context:component-scan /> to streamline development cycle.
4. @Autowired注解是在 Spring 2.5 上添加的,用于注解驱动的依赖注入。它与@Component注解和<context:component-scan />结合使用,以简化开发周期。
From Spring 3.0, Spring offers support for JSR-330 dependency injection annotations like @Inject, @Named, and @Singleton. It also added more Spring specific annotations like @Primary, @Lazy, and @DependsOn annotation.
从Spring 3.0开始,Spring提供了对JSR-330依赖注入注解的支持,如@Inject,@Named和@Singleton。它还添加了更多特定于 Spring 的注解,如 @Primary、@Lazy 和 @DependsOn 注解。
5. The @Inject annotation is good from the portability point of view. Since @Autowired is specific to the Spring framework, if you ever decided to move to Google Guice or any other dependency injection framework then you need to re-implement your dependency injection logic, even though your application remains the same. All bean creation logic needs to be changed to match with Google Guice’s implementation.
5.从可移植性的角度来看,@Inject注解很好。由于@Autowired特定于Spring框架,如果您决定迁移到Google Guice或任何其他依赖注入框架,那么即使您的应用程序保持不变,您也需要重新实现依赖注入逻辑。所有 Bean 创建逻辑都需要更改以匹配 Google Guice 的实现。
Here is an also nice summary of Spring annotations vs JSR 330 annotation to compare them side by side, this is useful to find an equivalent standard annotation for a spring-specific annotation e.g. you can use @Named in place of @Qualifier
, etc.
这里也是 Spring 注解与 JSR 330 注解的一个很好的摘要,可以并排比较它们,这对于找到特定于 Spring 的注解的等效标准注解很有用,例如您可以使用 @Named 代替 @Qualifier
等。
If you want to learn more then you can further join a comprehensive Spring course like Spring and Hibernate for Beginners to learn more about Spring-specific annotations, which are recently updated to cover Spring 5.0.
如果你想了解更多,那么你可以进一步加入一个全面的Spring课程,如Spring和Hibernate初学者,以了解更多关于Spring特定的注解,这些注解最近更新了Spring 5.0。
That’s all about the difference between @Inject and @Autowired annotation in the Spring framework. Remember, @Inject is a standard annotation defined by JSR-330 and @Autowired is spring specific. In theory, if you move to another DI framework like Google Guice, @Inject will work there.
这就是 Spring 框架中@Inject和@Autowired注解之间的区别。请记住,@Inject是 JSR-330 定义的标准注解,@Autowired特定于Spring。从理论上讲,如果你迁移到另一个像Google Guice这样的DI框架,@Inject将在那里工作。
Since Spring is the most popular DI and IOC container for Java application, @Autowired is more common and @Inject is lesser-known, but from a portability point of view, it’s better to use @Inject. Spring 3.0 supports JSR-330 annotation, so if you are using Spring 3.0 or higher release, prefer @Inject over @Autowired.
由于 Spring 是 Java 应用程序中最流行的 DI 和 IOC 容器,因此@Autowired更常见,@Inject鲜为人知,但从可移植性的角度来看,最好使用 @Inject。Spring 3.0 支持 JSR-330 注解,因此如果您使用的是 Spring 3.0 或更高版本,请更喜欢@Inject而不是@Autowired。
Author:javinpaul
FROM:https://medium.com/javarevisited/
文章评论