What is springBoot auto-configuration and what does it do?

In my opinion, the automatic configuration of SpringBoot is to add the beans in the third-party JAR package developed by others into the IOC container and manage these beans in a unified manner. When we develop business code, we only need to annotate @Component on the class to host the bean. However, if we use a third-party tool such as Mongo, we cannot put all the source code of Mongo into our own project and annotate them. But with auto-configuration, springBoot adds these third-party beans to the IOC container when the service is started, making it easy to use them. This is very convenient for developers, as long as the developers of these third-party tools follow springBoot’s principles and configure them properly, they can easily join springBoot hosting.

The principle of analysis

Let’s first look at springBoot’s boot class:

There’s a key annotation @springBootApplication. Let’s go ahead and look at what it’s made of:

EnableAutoConfiguration: @enableAutoConfiguration: @enableAutoConfiguration: @enableAutoConfiguration

Can be seen in the annotation of the above plus a @ Import annotations, and introduce a class called AutoConfigurationImportSelector, this class is the key, This class reads the configuration files of some third-party beans and adds them to ioc.

You can see intuitively that it implements a DeferredImportSelector and there’s a key method in it that we’re going to look at:

Yeah, that’s the point! This method returns an array of strings [], so you might be wondering, what does this array of strings store? Here’s what’s stored:Full path for many, many configuration classes!!Through this class bean management to a third party, this is a very important method, we continue to look down this method called getAutoConfigurationEntry dry what

This method calls the getCandidateConfiguration access toConfigured third-party beansSo let’s move on and see what’s going on in this method

Ok ok no nesting doll, go here SpringFactoriesLoader fromSpring.factoriesObtained in this configuration fileThe full path of many third-party configuration classes

This is it!! Let’s take a look at the configuration file: Let’s find springBoot’s autoconfiguration source package and see what’s in the configuration file:

Is that clear? This is the full path of many, many configuration classes that SpringBoot takes and manages in the IOC container.

Implement an automatic configuration

All right, so we’ve got the idea so let’s implement one manually

Suppose the class jojo is a third-party library. If we want to add it to IOC, then its configuration class must be as follows:

Then write a startup class:

You also need to write a key annotation: @enablejojoConfiguration

JojoConfiguration is the class that gets the joJO configuration class.

Here we implement ImportSelector and it doesn’t matter that the DeferredImportSelector interface in the source code is actually the inherited ImportSelector interface, and we get the configuration class of Jojo

Let’s start with JojoApplication:

You can see that the jojo class is already in the IOC container

Note: be sure to think of Jojo as a third party class. The above simple analysis of springBoot’s automatic configuration will also be implemented manually.