Injection and resource and auto-updated annotations

What is the difference between @Inject and @Resource and @Autowired ?

When should we use each of them?

+46
spring dependency-injection annotations cdi autowired
Dec 08 '13 at 7:20
source share
3 answers

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

@Autowired: spring likelihood annotations (as opposed to @Inject and @Resource) that introduce a resource by type, i.e. by interface class of the annotated field or contractor interface. If we have little implementation of the interface or subclass, we can narrow the selection using annotation @Qualifier to avoid ambiguity. For reciprocal matching, the bean name is considered the default qualifier value. Although you can use this convention to refer to specific beans by name, @Autowired is mainly related to input with type with additional semantic classifiers.

@Inject: JSR-330 (Dependency Injection for Java) annotations identify constructors, methods, and injection fields. This annotation is almost a complete replacement replacement for Spring s @ Automatic annotation. Thus, instead of using Spring-specific @Autowired annotations, you can use @Inject. One of the differences between @Autowired and @Inject is that @Inject does not have the required field, so if we fail to find a suitable input object, it will fail, and @Autowired may use required = false and allow the field with zero value (only if necessary!). The advantage of the @Inject annotation is that instead of entering the link directly, you can ask @Inject to enter the provider. The provider interface includes, among other things, lazy injection of bean links and insertion of multiple instances of bean. If we have few implementations of an interface or subclass, we can narrow down our choices using the @Named annotation to avoid ambiguity. @Named annotation works just like Spring s @Qualifier

@Resource: annotation based on JSR-250. @Resource is very similar to @Autowired and @Inject, but the main difference is the execution paths needed to find the required bean for input. @Resource narrows the search first by name, then by type and, finally, by Qualifications (ignored if a match is found by name). @Autowired and @Inject narrow down the search first by type, then by qualifier, and finally by name.

+92
Dec 08 '13 at 9:42
source share

In addition to @Haim's answer, there is a good description of the difference between Spring and JSR-330 annotations (Java dependency) and how to use the latter with Spring.

+1
Apr 18 '16 at 16:42 on
source share
 |------------|---------------|---------------|---------------|-----------------------| | | Setter/Field | Constructor | Applicable to | Matching order | | | injection | injection | type | | |------------|---------------|---------------|---------------|-----------------------| | @Resource | X | | X | Name, Type, Qualifier | |------------|---------------|---------------|---------------|-----------------------| | @Autowired | X | X | | Type, Qualifier, Name | |------------|---------------|---------------|---------------|-----------------------| | @Inject | X | X | | Type, Qualifier, Name | |------------|---------------|---------------|---------------|-----------------------| 
0
Nov 07 '17 at 12:40
source share



All Articles