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 questions on Spring interviews? Since everybody is now moved or moving to annotation driven, 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. The 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 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.

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 world, even though both @Autowired and @Inject serve the same purpose there are 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 read Spring in Action 4th Edition to learn more about Spring specific annotations.

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

That's all about 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 most popular DI and IOC container for Java application, @Autowired is more common and @Inject is lesser known, but from portability point of view, its 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
  • 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)

Here is a 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 reading the "Getting Started with Spring Framework" by Ashish Sarin.

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 Learn Spring Security course 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

No comments :

Post a Comment