This is the second day of my participation in the August More Text Challenge
Since the emergence of SpringBoot, the basic system construction process of Java has become easier, do not need to care about a lot of configuration files, do not need to care about the market of various versions of the package version conflict and so on, making SpringBoot developers highly respected.
SpringBoot
In the interview process, SpringBoot interview questions are also very frequent, simple point SpringBoot has what core notes? How to start SpringBoot? What are the differences between yML configuration files in SpringBoot? Wait, this is all testing your knowledge of SpringBoot.
Of course, there are some more detailed interview questions like how does SpringBoot implement hot deployment? How to set the service port number for SpringBoot? How does SpringBoot switch configuration files in different environments? These questions are different. If the interviewer asks you these questions, you may be testing whether you have actually used SpringBoot, or whether you have a certain foundation in the SpringBoot program.
In addition to the interview questions above, there will be some questions to test whether you have studied SpringBoot more deeply than just the usage level, such as: How does SpringBoot auto-assemble? Starter principle in SpringBoot? And so on.
Today we are talking about the principle of automatic assembly in SpringBoot, automatic assembly is a very important SpringBoot, is also a very characteristic function, let’s take a look at it.
Automatically.
Start with the startup class
Speaking of autowiring in SpringBoot, we should start with the boot class of SpringBoot.
@SpringBootApplication
public class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code
This is a basic SpringBoot boot class, we mainly look at the Annotation of SpringBootApplication, click on the source code, see the concrete implementation.
Based on the information in the figure, we can clearly see that the annotation SpringBootApplication is a composite annotation.
The SpringBootConfiguration annotation might be a bit strange, but not for @configuration; ComponentScan is also a scanning annotation that we often use in Spring projects.
Let’s talk about @enableAutoConfiguration annotation, automatic configuration annotation, also can say automatic assembly, since it is to talk about the principle of automatic assembly, that is to talk about the concrete implementation of @enableAutoConfiguration annotation, let’s look at it.
Enable the @enableAutoConfiguration annotation
Take a look at the source implementation of this annotation, as shown below.
In this annotation implementation, start with the AutoConfiguration annotation.
If you have written custom annotations, you will be able to distinguish between these concepts.
@target ({elementtype.type}) : used to declare the scope of an annotation.TYPE means that you can modify interfaces, classes, enumerations, annotations. There are other enumerated types that you can search for on your own if you are interested. @Retention(retentionPolicy. RUNTIME) : Used to declare the lifetime of annotations.RUNTIME means: valid at RUNTIME. Documented: @Documented: Documented annotation. If modified by this annotation, Documented in Javadoc, is a public API. @Inherited: Used to strengthen inheritance.Copy the code
Let’s get back to the point.
The AutoConfiguration annotation is also not an auto-assembly annotation. In Spring, it is used to scan for annotations such as the Entity class Entity.
Import annotations are the core of autowiring.
Import annotations implements AutoConfigurationImportSelector class, automatic assembly is also in this class has carried on the concrete implementation.
AutoConfigurationImportSelector class implements the method, the realization of the automatic assembly is in selectImports approach, as shown.
Read from the source, there is a getCandidateConfigurations method, enter a look at the code.
The “spring.factories” file is loaded with an assertion that the “spring.factories” file is not found correctly.
Search through the current class location button in IDEA,
The file can be found here, as shown below:
The file content is shown as follows:
As you can see, the selectImports method takes the names of a series of classes under this file and then automatically loads them into the IOC container.
All of these classes have automatic configuration. You can also find that the names of these classes end with AutoConfiguration.
conclusion
Principle of automatic assembly already had said, to summarize, is through the @ EnableAutoConfiguration annotations, loading AutoConfigurationImportSelector selectImports method in class, The auto-configuration classes in the Spring. factories file in mate-INF are scanned and assembled into the IOC container.