66

What's the difference between @Inject and @Resource and @Autowired annotations?

When should we use each of them?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
oxygenan
  • 1,023
  • 2
  • 12
  • 21
  • 2
    http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/htmlsingle/#beans-standard-annotations, http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/htmlsingle/#beans-resource-annotation – JB Nizet Dec 08 '13 at 08:04
  • 1
    possible duplicate of [@Resource vs @Autowired](http://stackoverflow.com/questions/4093504/resource-vs-autowired) – Christian Strempfer Aug 06 '15 at 07:22
  • 1
    Nice explanation with examples i found is [here](http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/) – marioosh Sep 26 '15 at 13:07

3 Answers3

129

The difference between @Inject vs. @Autowire vs. @Resource?

@Autowired: spring propriety annotation (as opposed to @Inject and @Resource) that inject a resource by-type, i.e. by the class of by the interface of the annotated field or contractor. In case we have few implementation of an interface or a subclass we can narrow down the selection using the @Qualifier annotation to avoid ambiguity. For a fallback match, the bean name is considered a default qualifier value. Although you can use this convention to refer to specific beans by name, @Autowired is fundamentally about type-driven injection with optional semantic qualifiers.

@Inject: Annotation based on JSR-330 (Dependency Injection for Java) identifies injectable constructors, methods, and fields. This annotation is an almost complete drop-in replacement for Spring’s @Autowired annotation. So, instead of using the Spring-specific @Autowired annotation, you might choose to use @Inject. One of the differences between @Autowired and @Inject is that @Inject does not have the required field so in case we fail to find a suitable object to inject it will fail while @Autowired can used required=false and allow null able field (only if required!). Advantage of @Inject annotation is that rather than inject a reference directly, you could ask @Inject to inject a Provider. The Provider interface enables, among other things, lazy injection of bean references and injection of multiple instances of a bean. In case we have few implementation of an interface or a subclass we can narrow down the selection using the @Named annotation to avoid ambiguity. @Named annotation works much like Spring’s @Qualifier

@Resource: annotation based on JSR-250. @Resource is quite similar to @Autowired and @Inject, but the main difference is the execution paths taken to find out the required bean to inject. @Resource will narrow down the search first by name then by type and finally by Qualifiers (ignored if match is found by name). @Autowired and @Inject will narrow down the search first by type then by qualifier and finally by the name.

Haim Raman
  • 11,508
  • 6
  • 44
  • 70
  • 6
    Can you say something about what is recommended? Best practice? – Jan-Terje Sørensen Oct 28 '14 at 07:11
  • 1
    I would be happy to response on your comment, but I prefer to keep this answer as is. Would you post a new question. – Haim Raman Nov 03 '14 at 11:30
  • 1
    In last line, you have mentioned @Autowired also search by name, as I know It doesn't have name option, does it ? – Ramesh Karna Dec 27 '14 at 06:03
  • 1
    See my update to the @Autowired description. from the docs http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html "For a fallback match, the bean name is considered a default qualifier value." – Haim Raman Dec 27 '14 at 08:36
  • 2
    @Jan-TerjeSørensen I would advice use framework independent `@Inject` annotation everywhere. Cause of if you decide to change Spring to e.g. Google Guice (or any another JSR-330 DI framework) you wouldn't change you code. – Andriy Kryvtsun Apr 18 '16 at 16:35
21
|------------|---------------|---------------|---------------|-----------------------|
|            | Setter/Field  | Constructor   | Applicable to | Matching order        |
|            | injection     | injection     | type          |                       |
|------------|---------------|---------------|---------------|-----------------------|
| @Autowired |       X       |       X       |               | Type, Qualifier, Name |
|------------|---------------|---------------|---------------|-----------------------|
| @Inject    |       X       |       X       |               | Type, Qualifier, Name |
|------------|---------------|---------------|---------------|-----------------------|
| @Resource  |       X       |               |       X       | Name, Type, Qualifier |
|------------|---------------|---------------|---------------|-----------------------|

So in Spring dependency injection @Inject and @Autowired have exactly the same behaviour.

eztam
  • 3,443
  • 7
  • 36
  • 54
  • 1
    What does "applicable to type" column signifies in the table? – RBz Oct 23 '18 at 12:19
  • 1
    @RBz See: https://docs.oracle.com/javaee/6/api/javax/annotation/Resource.html "If the annotation is applied to the component class, the annotation declares a resource that the application will look up at runtime." But I'm not sure if this would also work with Spring DI. – eztam Oct 23 '18 at 14:04
1

In addition to @Haim answer there is good description of the difference between Spring and JSR-330 (Dependency Injection for Java) annotations and how to use the last with Spring.

Andriy Kryvtsun
  • 3,220
  • 3
  • 27
  • 41