This is the 19th day of my participation in the August More Text Challenge

When developing with SpringBoot, Java Config is used more often than XML. The @AutoWired annotation must be used to inject the related bean object in the container. Today we will look at how the @AutoWired annotation works.

Learn @AutoWired annotations and use them with SpringBoot custom annotations.

@autowired annotation definition

First, let’s look at the @autowired annotation. The source code looks like this:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
    boolean required(a) default true;
}
Copy the code

As you can see from the definition information in the annotation,

  1. There are five types used in the @target annotation for where the @AutoWired annotation can be used, each representing the following:
    • ElementType.FIELD: used for field declaration
    • ElementType.METHOD: for method declarations
    • ElementType.PARAMETER: used for formal parameter declarations
    • ElementType.CONSTRUCTOR: for constructor declarations
    • ElementType.ANNOTATION_TYPE: used to annotate type declarations
  2. use@Retention(RetentionPolicy.RUNTIME)Indicates that the current annotation exists at compile and run time and can be retrieved backwards.
  3. Annotations have attribute valuesrequiredTo indicate whether the current content to be injected is required
    • A value of true indicates that it must, and an error is reported if the specified type is not found;
    • If the value is false, it is not necessary, and no error is reported if no type to be injected is found.
    • The default value is false.

Implementation logic for @autowired annotations

The basic logic of annotation implementation

First let’s consider the basic logic of annotations in Spring. Since annotations are implemented based on reflection, there are the following steps:

  1. Custom annotation
  2. Implementing annotation logic
  3. Use annotations to annotate properties, methods, constructors, etc
  4. During class initialization, all attributes, methods, constructors, etc. of the class are checked to see if the annotation is used, and if so, the attribute is put into the collection
  5. After finding all the properties that use the annotation, find the corresponding bean in the SpringContext and initialize the return

Implementation of @autowired annotations

  1. Find the @AutoWired annotation definition location. By locating the @AutoWired annotation, you can find the @AutoWired annotation stored inorg.springframework.beans.factory.annotationIn the package.
  2. Let’s look at the implementation logic of the @AutoWired annotation, andAutowiredAnnotationBeanPostProcessorThe Autowired post-assembly processor is used to implement the concrete logic of the @AutoWired annotation.
  3. inAutowiredAnnotationBeanPostProcessorClass gets all the attributes and methods of the bean, determines whether it uses the @AutoWired annotation, and wraps the attribute information that requires dependency injection into theInjectionMetadataIn the class.
  4. Finally, the bean corresponding to the property information is injected. When the bean is created (instantiating the object and initializing it), various calls are madeBeanPostProcessorInitialize the bean,AutowiredAnnotationBeanPostProcessorResponsible for injecting related dependencies.

The last

Today’s learning of the @AutoWired annotation implementation is just a relatively simple process, to sum up: For all beans injected with @AutoWired, the code structure is a normal member variable. Spring will use reflection to find the member variable inside the container and assign it to the desired class instance before completing the injection process.