First, the @autowired

1, the @autowired is the annotations, spring through ‘AutowiredAnnotationBeanPostProcessor class implements the dependency injection;

2, @Autowired is automatically assembled according to the type. If multiple types are found with the same type, they will be matched by the name; if the name is the same, an error will be reported. If an assembly by the specified name is required, @Qualifier will be matched;

3, @Autowired has an attribute of required, which can be configured to false. If it is configured to false, the system will not commit an error if the corresponding bean is not found;

4, @Autowired can be used on variables, setter methods, and constructors.

(a) The @autowored code is written on injected member variables, it is not configured in XML files, it is stripped of setters and getters in programs,

B. You can also write on constructors and setters

C, @ the Qualifier

The XX in @Qualifier(“XXX”) is the name of the Bean, so when @Autowired and @Qualifier are used in combination, the automatically injected policy changes from ByType to ByName.

Note, however, that @Autowired can annotate member variables, methods, and constructors, while @Qualifier’s annotation objects are member variables, method parameters, and constructor parameters.

Second, @ Inject

1, @Inject is a specification in JSR330 (Dependency Injection for Java) and needs to import javax.Inject.Inject. Implement injection.

2. @Inject is automatically assembled according to type. If you need to assemble by name, you need to cooperate with @Named;

@Inject works on variables, setter methods, and constructors.

A, @Inject can be applied to variables, setter methods, and constructors, as can @Autowired

B, @ Named

The XX in @named (“XXX”) is the name of the Bean, so when @Inject is used in combination with @named, the auto-injection strategy changes from ByType to ByName.

Third, @ the Resource

1, @Resource is the implementation of JSR250 specification, need to import javax.annotation to implement injection.

2, @Resource is auto-assembled by name, usually with a name attribute specified

3. @Resource can be used on variables and setter methods.

A, @Resource instance

Conclusion:

1. @Autowired is provided by Spring, @Inject is implemented by the JSR330 specification, and @Resource is implemented by the JSR250 specification. Different packages need to be imported

2, @Autowired and @Inject are basically the same, except that @Autowired has a request property

3, @Autowired, @Inject are matched by type by default, and @Resource by name

4, @Autowired, if required to match by Name, should be used together with @Qualifier and @Inject and @Name