java - works - What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?
when to use @inject (7)
I am going through some blogs on SpringSource and in one of the blogs, author is using
@Inject and I suppose he can also use
Here is the piece of code:
@Inject private CustomerOrderService customerOrderService;
I am not sure about the difference between
@Autowired and would appreciate it if someone explained their difference and which one to use under what situation?
@Autowired annotation is defined in the Spring framework.
@Inject annotation is a standard annotation, which is defined in the standard "Dependency Injection for Java" (JSR-330). Spring (since the version 3.0) supports the generalized model of dependency injection which is defined in the standard JSR-330. (Google Guice frameworks and Picocontainer framework also support this model).
@Inject can be injected the reference to the implementation of the
Provider interface, which allows injecting the deferred references.
@Autowired- is almost complete analogies. As well as
@Inject annotation can be used for automatic binding properties, methods, and constructors.
In contrast to
@Inject annotation has no
required attribute. Therefore, if the dependencies will not be found - will be thrown an exception.
There are also differences in the clarifications of the binding properties. If there is ambiguity in the choice of components for the injection the
@Named qualifier should be added. In a similar situation for
@Autowired annotation will be added
@Qualifier qualifier (JSR-330 defines it's own
@Qualifier annotation and via this qualifier annotation
@Named is defined).
@Inject has no 'required' attribute
Assuming here you're referring to the
@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
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.
Better use @Inject all the time. Because it is java configuration approach(provided by sun) which makes our application agnostic to the framework. So if you spring also your classes will work.
If you use @Autowired it will works only with spring because @Autowired is spring provided annotation.
In addition to the above:
- The default scope for @Autowired beans is Singleton whereas using JSR 330 @Inject annotation it is like Spring's
- There is no equivalent of @Lazy in JSR 330 using @Inject
- There is no equivalent of @Value in JSR 330 using @Inject
@Inject annotation is one of the JSR-330 annotations collection. This has Match by Type,Match by Qualifier, Match by Name execution paths.
These execution paths are valid for both setter and field injection.The behavior of
@Autowired annotation is same as the
@Inject annotation. The only difference is the
@Autowired annotation is a part of the Spring framework.
@Autowired annotation also has the above execution paths. So I recommend the
@Autowired for your answer.
To handle the situation in which there is no wiring, beans are available with
required attribute set to
But when using
@Inject, the Provider interface works with the bean which means that the bean is not injected directly but with the Provider.