top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between @Autowired and @Inject annotation in Spring?

+1 vote
730 views
What is the difference between @Autowired and @Inject annotation in Spring?
posted Nov 2, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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 difference between these two annotations is that unlike Spring's @Autowired, the @Inject does require the 'required' attribute.

3) The 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 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.

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.

answer Nov 3, 2017 by Manikandan J
...