Detailed explanation of the implementation principle of spring automatic assembly of Java annotation mechanism
Annotations are mainly used in spring MVC (spring boot, etc.) in Java. Annotations are actually equivalent to a markup language, which allows you to dynamically operate the members with the tag at runtime. Note: the spring framework does not support automatic assembly by default. To use automatic assembly, you need to modify the autowire attribute of the < bean > tag in the spring configuration file.
The auto assembly attribute has 6 optional values, representing different meanings:
Byname - > when obtaining the target object from the spring environment, the attribute in the target object will find the ID attribute value of the < bean > tag in the whole spring environment according to the name. If there are the same, get the object and implement the association. The whole spring environment: it means to search in all spring configuration files, so there can be no duplicate IDs.
Bytype - > when getting the target object from the spring environment, the properties in the target object will find the class attribute value of the < bean > tag in the whole spring environment according to the type. If there are the same, get the object and implement the association.
Disadvantages: if there are multiple bean objects of the same type, an error will occur; If the attribute is a single type of data, an error will occur when multiple associated objects are found; If the property is of array or collection (generic) type, no exception occurs when multiple associated objects are found.
Constructor - > using the construction method to complete object injection is actually to find objects according to the parameter type of the construction method, which is equivalent to using bytype.
Autodetect - > auto select: if the object does not have a parameterless construction method, the automatic assembly method of constructor is automatically selected for construction injection. If the object contains a parameterless construction method, the automatic assembly method of bytype is automatically selected for setter injection.
No - > auto assembly is not supported
Default - > indicates the value of automatic assembly using the upper level label by default. If there are multiple profiles, the automatic assembly method of each profile is independent.
Three conditions are required for annotation use, including annotation declaration, element using annotation, and operation of code using annotation element. The first step is annotation declaration. Annotation is a type. The writing code of custom annotation is as follows:
The user-defined annotation element is used, and the code is as follows:
The test operation executes the main function. The specific code is as follows:
Execution results:
Attribution: Henan Province attribution: Hebei Province attribution: Hebei Province
Spring operates in two ways to facilitate automatic assembly: Inheriting org springframework. web. context. support. Springbeanautowiringsupport class or add @ component / @ controller and other annotations and declare the context: component scan element configuration in the spring configuration file.
1) To realize automatic assembly by inheritance, see spring 3 1.1 in the source code, you will find the following code in the springbeanautowiringsupport class:
Analysis: Java will call the default parent class parameterless construction method when instantiating the construction, and spring makes the operation element code execute through this point.
2) The annotation method is also similar to the above theory. It is worth noting that annotation automatic assembly does not need to complete the injection setter *. See spring 3 1.1 the calling order of source code annotation is: org springframework. web. context. support. SpringBeanAutowiringSupport#SpringBeanAutowiringSupport=> org. springframework. web. context. support. SpringBeanAutowiringSupport#processInjectionBasedOnCurrentContext=> org. springframework. beans. factory. annotation. AutowiredAnnotationBeanPostProcessor#processInjection=> org. springframework. beans. factory. annotation. Injectionmetadata #injection. Check the source code of the inject method as follows:
Analysis: through the above source code, spring automatic assembly is realized through reflection mechanism.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.