Difference between @Autowired and @Inject annotations in Spring Framework?
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.
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.
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.
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.
You can potentially avoid that development effort by using standard annotations specified by JSR-330 e.g. @Inject, @Named, @Qualifier, @Scope, and @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.
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.
What is difference between @Autowired vs @Inject Annotations?
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.
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
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.
2. The second difference between these two annotations is that, unlike Spring’s @Autowired, the @Inject does not require the ‘required’ attribute.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Further Reading
Spring and Hibernate for Beginners
Spring Master Class — Beginner to Expert
Spring in Action 5th Edition
Introduction to Spring MVC 4 By Bryan Hansen
Other Spring tutorials and Interview Questions you may like
- Top 5 Courses to learn Spring Framework for Beginners (courses)
- 20+ Spring Boot Interview Questions with Answers (questions)
- 5 Books to Learn Spring framework for Java developers (books)
- 23 Spring MVC Questions from Java Interviews (list)
- 10 Advanced Spring Boot Courses for Experienced Developers (best courses)
- 15 Microservice Interview Questions with Answers (questions)
- How to call a stored procedure from Java using Spring? (tutorial)
- 17 Spring AOP Interview Questions with Answers (questions)
- How to consume JSON from RESTful Web Service using Spring? (tutorial)
- 5 Spring and Hibernate training courses for experienced Java programmers (courses)
- How to implement Role-based Access Control using Spring Security? (guide)
- How to access a JNDI DB connection pool in Tomcat using Spring? (guide)
- 15 Spring Data JPA Interview Questions with Answers (questions)
- How to setup LDAP authentication using Spring Security? (guide)
- Top 5 Courses to learn Spring Boot for Java developers (courses)
- 25+ Spring Security Interview Questions with Answers (questions)
P. S. — If you are an absolute beginner on the Spring framework and want to start in the right way i.e. by using Java configuration and annotation, I suggest you join Spring Framework 5: Beginner to Guru by John Thompson.
P. S. — If you are an experienced Java/JEE Program and want to learn advanced Spring concepts e.g. Spring Security or REST with Spring then I recommend REST with Spring and Learn Spring Security masterclass courses by Eugen Paraschiv, The definitive guide to secure your Java application. It’s useful for both junior and experienced Java Web developers. You can access it here.