Friday, October 19, 2018

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 Spring framework. The @Autowired annotation is used for auto-wiring in Spring framework. If you don't know, autowiring is a process on which Spring framework figure out 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 which 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.

A 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 course to start with Spring. It's also covers Spring 5.0, the latest version 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 framework e.g. 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 require the 'required' attribute.

3) The third most common difference between @Autowired and @Inject annotation is that former is Spring specific while later 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 e.g. @Inject@Named, and @Singleton. It also added more Spring specific annotations e.g. @Primary@Lazy, and @DependsOn annotation.

You can further read Spring in Action 5th Edition to learn more about Spring specific annotations, which is recently updated to cover Spring 5.0.

@Autowired vs @Inject annotation in Spring



5) The @Inject annotation is good from the portability point of view. Since @Autowired is specific to 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 same. All bean creation logic needs to be changed to match with Google Guice's implementation.


Further Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Introduction to Spring MVC 4 By Bryan Hansen
Spring and Hibernate for Beginners


That's all about the difference between @Inject and @Autowired annotation in 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)
  • 5 Books to Learn Spring framework for Java developers (books)
  • 23 Spring MVC Questions from Java Interviews (list)
  • 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 call a stored procedure from Java using Spring? (tutorial)
  • How to access a JNDI DB connection pool in Tomcat using Spring? (guide)
  • How to setup LDAP authentication using Spring Security? (guide)
  • Top 5 Courses to learn Spring Boot for Java developers (courses)

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


P.S. - If you are an absolute beginner on 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

1 comment :

Unknown said...



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.

Post a Comment