Wednesday, May 15, 2024

Difference between @Autowired and @Inject annotation in Spring?

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 that's why you should know what they are and when to use them. It will also allow you to make effective use of Spring framework. 



Difference between @Autowired vs @Inject Annotation?

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.

Difference between @Autowired and @Inject annotation in Spring framework



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.



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)

7 comments:



  1. Assuming here you're referring to the javax.inject.Inject annotations. @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 @Inject synonymously with their own @Autowired annotation.

    So, to answer your question, @Autowired is Spring's own (legacy) annotation. @Inject is part of a new Java technology called CDI that defines a standard for dependency injection similar to Spring. In a Spring application, the two annotations works the same way as Spring has decided to support some JSR-299 annotations in addition to their own.

    ReplyDelete
  2. Summary: Use @Inject because it is newer, shiner, and works for other stuff, not just Spring, unless you are stuck with old Spring, which in case, sorry, use @Autowired

    ReplyDelete
  3. Same purpose but @Inject is under javax library used also in JakartaeEE/JavaEE while @Autowired is part of MVC Spring module

    ReplyDelete
  4. Hello Javin ,
    Nice article keep up the good work , Just to add @Inject is part of the Java CDI (Contexts and Dependency Injection) standard introduced in Java EE 6 (JSR-299),Spring has chosen to support using @Inject synonymously with their own @Autowired annotation

    @Autowired is Spring's own (legacy) annotation. @Inject is part of a new Java technology called CDI that defines a standard for dependency injection similar to Spring. In a Spring application, the two annotations works the same way as Spring has decided to support some JSR-299 annotations in addition to their own

    ReplyDelete
  5. For point no. 2, It would be @Inject does "not" require the 'required' attribute.

    ReplyDelete
  6. Thanks for pointing @Saheb, will correct it.

    ReplyDelete
  7. This post compares apples with pears as @Inject is not a Spring annotation. So the question of the difference between @Autowired and @Inject in Spring doesn't make sense 'cause only @Autowired is Spring. Hence, one could say that the difference between @Autowired and @Inject in Sping is @Autowired as we're talking here about two disjoints sets.

    ReplyDelete