66

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

When should we use each of them?

3

3 Answers 3

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.

5
  • 6
    Can you say something about what is recommended? Best practice? Commented Oct 28, 2014 at 7: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
    Commented Nov 3, 2014 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 ? Commented Dec 27, 2014 at 6:03
  • 1
    See my update to the @Autowired description. from the docs docs.spring.io/spring/docs/current/spring-framework-reference/… "For a fallback match, the bean name is considered a default qualifier value."
    – Haim Raman
    Commented Dec 27, 2014 at 8: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. Commented Apr 18, 2016 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.

2
  • 1
    What does "applicable to type" column signifies in the table?
    – RBz
    Commented Oct 23, 2018 at 12:19
  • 1
    @RBz See: 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
    Commented Oct 23, 2018 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.