First of all, for a SpringBoot project, the most obvious sign is @SpringBootApplication which marks that this is a SpringBoot project, so today’s SpringBoot automatic assembly principle is to start from it.

Automatic assembly process

Let’s take a look at the @springBootApplication annotation and see what’s behind it. Press CTRL + the left mouse button, click the button, and see the magic moment. We see elegant code like this:

The @springBootConfiguration annotation and the @enableAutoConfiguration annotation are two of the most noticeable. The reason why these two annotations attract our attention is not because they are pretty, but because the other annotations are ugly (mainly because we are familiar with the other annotations, even if we don’t know what they are, we can be sure that it doesn’t matter if they are more auto-assembled). Then we stretched out the evil hands, opened the familiar operation, press Ctrt + left mouse button, staring colored Mimi small eyes, pupil dilated a hundred times waiting for the miracle… Wipe… Wipe… Wipe…

Nothing… Then why do I need you, such a top world-class open source project, how can let a useless guy exist? It took an army of hundreds of millions of brain cells and a complex calculation to come to the dubious conclusion that it might be used to mark the configuration as a SpringBoot project. Because SpringBootConfiguration translates to the configuration of SpringBoot, tens of thousands of alpacas are flying in the desert.

Calm, adhering to the belief that failure is the mother of success, the skilled left hand press Ctrl + Table, back to the original place. Keep your eyes on @enableAutoConfiguration, look around, type In Google Translate in the address bar, and the result shows autoassembly. I am looking for you, is really in the search for him thousands of baidu, that person is dim lights. Skilled press Ctrl + left, can’t wait to enter; Silently recited the heart of the “Peach Blossom Land” classic poem:

When the forest had exhausted its waters, there was a mountain with a small opening, as if there were light. Then he left the boat and entered through the mouth. The beginning of the extreme narrow, only the general. This is a happy moment, after the previous experience, in the face of the new world, a lot of calm. The brain at high speed, no tangle, taking the huanglong, entered the AutoConfigurationImportSelector. Class class, because Google translate tells us that it is automatically configured import selector. So we found a new world

view code
Copy the code

The AutoConfigurationEntry has two properties, Configurations and Exclusions.

view code
Copy the code

At the back can see getAutoConfigurationEntry () method returns an object return new AutoConfigurationEntry (configurations, exclusions); So here we have all the configuration we need.

So how did he get the candidate configuration classes? We went on to look at this to get the candidate List method configuration class configurations = getCandidateConfigurations (annotationMetadata, attributes); Going into the method, we see the following method that concretely retrieves the method content of the candidate configuration class

Here we follow the breakpoint to first enter getSpringFactoriesLoaderFactoryClass () method

view code
Copy the code

Next we go to the getBeanClassLoader() method, which is a classloader

view code
Copy the code

Finally, we go to the loadFactoryNames() method, which finds the candidate configuration class based on the bytecode file and the classloader. The bytecode passed in

view code
Copy the code

The diagram below:

Finally, get all the configuration classes using loadSpringFactories()

view code
Copy the code

Here we’ll look at how FACTORIES_RESOURCE_LOCATION is loaded from the resources directory. Here is the path to load the configuration file:

The “SpringBoot” project starts with all the Spring. factories under the meta-INF file. The “SpringBoot” project starts with all the Spring. factories under the meta-INF file. The “SpringBoot” project starts with all the Spring. factories under the meta-INF file

But the last auto-assembled class is this spring-boot-Autoconfigure-2.4.3.release.jar

And according to EnabLeAutoConfiguration. The class bytecode loaded configuration classes and there were only 118 automatic configuration

summary

The SpringBoot autowage principle, which is to load the Spring. factories in meta-INF at startup, is not that elegant. Of course, other configuration items will be loaded during the startup process, so we will talk about the auto assembly loading process. I hope to inspire you.

The original link: www.cnblogs.com/reminis/p/1…

Three things to watch ❤️

If you find this article helpful, I’d like to invite you to do three small favors for me:

1. Like, forward, with your “like and comment”, is the power of my creation.

2. Follow the public account “Wish heaven has no BUG” and share original knowledge from time to time. Also look forward to the follow-up article ing

3. Reply [Learning] scan the code to obtain the learning materials package