Spring @autowired annotation

1. In the interface implementation class, the @Autowired annotation is used, and the following is the class used by the annotation

2. Spring@Autowired annotation and automatic assembly

@Autowired, as the name suggests, is automatic wiring, its function is to eliminate the getter/setter in the code Java code and the property in the bean property. Of course, the getter depends on personal needs. If private properties need to be provided externally, they should be reserved.

 

This annotation is that spring can automatically help you omit the setter/getter method of the object referenced in the bean, and it will automatically set/get for you.

 

In fact, when spring IoC is started, the container automatically loads an AutowiredAnnotationBeanPostProcessor post-processor. When the container scans @Autowied, @Resource or @Inject, it will automatically find the required bean in the IoC container and assemble the properties of the object.

 

3. @Autowired can annotate member variables, methods, and constructors. So what's the difference between annotating member variables and constructors?

Order resolution of @Autowired and constructor execution

  First look at a piece of code, can the following code run successfully?

 @Autowired
 private User user;
 private String school;

 public UserAccountServiceImpl(){
     this.school = user.getSchool();
 }

  The answer is no.

  Because the Java class will execute the constructor first, and then inject values ​​into the user annotated with @Autowired, an error will be reported when the constructor is executed.

  The error message may look like the following: 

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name ‘...‘ defined in file [....class]: Instantiation of bean failed; 
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[...]: Constructor threw exception; nested exception is java.lang.NullPointerException

The error message says: An error occurred while creating the bean. The reason for the error is that the instantiation of the bean failed, because an error occurred in the constructor of the bean, and a null pointer exception was thrown in the constructor.

  The solution is to use constructor injection, as follows:

 private User user;
 private String school;

 @Autowired
 public UserAccountServiceImpl(User user){
     this.user = user;
     this.school = user.getSchool();
 }

 It can be seen that by using the method of constructor injection, the loading order of member variables can be specified.

  PS: The initialization sequence of Java variables is: static variable or static statement block –> instance variable or initialization statement block –> constructor –> @Autowired

      @Autowired itself is a singleton mode. It will only be executed once when the program starts. Even if final is not defined, it will not be initialized for the second time, so this final is meaningless.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324983660&siteId=291194637