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,
- 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 declarationElementType.METHOD
: for method declarationsElementType.PARAMETER
: used for formal parameter declarationsElementType.CONSTRUCTOR
: for constructor declarationsElementType.ANNOTATION_TYPE
: used to annotate type declarations
- use
@Retention(RetentionPolicy.RUNTIME)
Indicates that the current annotation exists at compile and run time and can be retrieved backwards. - Annotations have attribute values
required
To 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:
- Custom annotation
- Implementing annotation logic
- Use annotations to annotate properties, methods, constructors, etc
- 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
- After finding all the properties that use the annotation, find the corresponding bean in the SpringContext and initialize the return
Implementation of @autowired annotations
- Find the @AutoWired annotation definition location. By locating the @AutoWired annotation, you can find the @AutoWired annotation stored in
org.springframework.beans.factory.annotation
In the package. - Let’s look at the implementation logic of the @AutoWired annotation, and
AutowiredAnnotationBeanPostProcessor
The Autowired post-assembly processor is used to implement the concrete logic of the @AutoWired annotation. - in
AutowiredAnnotationBeanPostProcessor
Class 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 theInjectionMetadata
In the class. - Finally, the bean corresponding to the property information is injected. When the bean is created (instantiating the object and initializing it), various calls are made
BeanPostProcessor
Initialize the bean,AutowiredAnnotationBeanPostProcessor
Responsible 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.