Spring Boot is much simpler than Spring because it allows you to build a project with more than one starter, so you can use it directly without requiring a lot of configuration. Spring officially provides many starter, and we can customize our own starter, in order to be able to distinguish, from the specification of the name on the starter. The official starter name is spring-boot-starter- XXX, and the customized starter name is xxx-spring-boot-starter.

Spring Boot helps simplify development by providing startup dependencies and auto-configuration.

Starting dependencies are essentially what we write in Maven as dependency coordinates, which makes it easier to package and import coordinates that have a certain functionality.

Automatic configuration is the core of Spring Boot. The spring-boot-starter is essentially the automatic configuration of Spring Boot. Automatic configuration, which means that you don’t have to manually configure the XML, but automatically configure and manage the beans, can simplify the development process. Automatic configuration has the following key steps:

Java code-based Bean configuration

Automatically configure conditional dependencies

Bean parameter acquisition

The discovery of the Bean

The Bean’s loading

The following is a starter example myBatis -spring-boot-starter to describe the automatic configuration process.

Java code-based Bean configuration

When we import the coordinates of the MyBatis -spring-boot-starter JAR through Maven, we can see that the jar contains many related jars, as shown in the figure below:

The mybtis-spring-boot-Autoconfigure jar package is called “autoconfigure”. This jar is called “mybtis-spring-boot-Autoconfigure”. This jar is called “autoconfigure”. MybatisAutoConfiguration (MybatisAutoConfiguration)

Let’s take a look at the key code from the class:

As you can see, there are a lot of annotations in this class, and indeed the implementation of auto-configuration is basically based on these annotations.

The @Configuration and ** @bean annotations are used together to create a Java-based Configuration class that can be used as an alternative to traditional XML Configuration files.

The @configuration class indicates that this class is a Configuration class. This annotated class can be seen as a factory that produces Bean instances managed by the Spring IoC container. In other words, this class can instantiate objects and place them in the Spring container instead of the old XML Configuration file.

The @bean annotation means that the object returned by the method can be registered with the Spring container, which means it instantiates an object and puts it in the Spring container.

The MybatisAutoConfiguration class in the figure above automatically generates important instances of Mybatis such as SqlSessionFactory and SqlSessionTemplate and gives them to the Spring container to manage, thus completes the automatic registration of beans.

Automatically configure conditional dependencies

The MybatisAutoConfiguration class uses annotations that include **@Configuration and @bean ** as well as other annotations that are dependent on the autoconfiguration.

The meaning of these two annotations is to complete Mybatis automatic configuration, need to exist in the classpath SqlSessionFactory class, SqlSessionFactoryBean. Class these two classes, You also need to have a DataSource bean which is the DataSource and you need to inject the object into it and the bean is automatically registered.

Here are some common conditional dependencies:

annotations

Functional specifications

@ConditionalOnBean

A bean is instantiated only if it exists in the current context (the Spring container)

@ConditionalOnClass

The Bean is instantiated only if a class is on the classpath

@ConditionalOnExpression

The Bean is instantiated only when the expression is true

@ConditionalOnMissingBean

A bean is instantiated only if it does not exist in the current context (the Spring container)

@ConditionalOnMissingClass

The Bean is instantiated when a class does not exist on the classpath

@ConditionalOnNotWebApplication

This Bean is instantiated only when it is not a Web application

@AutoConfigureAfter

Instantiate a bean after it has been autoconfigured

@AutoConfigureBefore

Instantiate a bean before it is autoconfigured

Bean parameter acquisition

In the project, we need to provide the configuration parameters related to the data source in the configuration file to complete the automatic configuration of mybatis through Spring Boot, such as database driver, connection URL, database user name, password and so on. Let’s look at how Spring Boot reads properties from yML or properites configuration files to create data source objects.

When we import the Mybatis -spring-boot-starter JAR, we also import a spring-boot-autoconfigure package. Point under the open the package of JDBC folder has a an automatic configuration class DataSourceAutoConfiguration, as shown in figure:

The automatic configuration classes to add * * * * @ EnableConfigurationProperties annotations, the annotations on the meaning of is to enable configuration properties, is to enable the inside of the parentheses in the class attribute, we open the class in parentheses:

You can see that the **@ConfigurationProperties annotation has been added to this class, The function of this annotation is to encapsulate the configuration parameter information from the YML or Properties configuration file to the corresponding property of the @ConfigurationProperties annotation bean(DataSourceProperties**).

@ * * * * EnableConfigurationProperties annotations of the actual effect is to make the * * * * @ ConfigurationProperties annotations to take effect.

The discovery of the Bean

By default, Spring Boot scans all components in the main and subclasses of the package in which the starter class is located. It does not include dependency packages, which are classes in the various starter classes. Here’s a look at how beans in the starter class are discovered.

** @springBootApplication ** is an annotation for the class we create for the SpringBoot project. Let’s see how the bean is discovered:

Click on it and you’ll see three important notes:

@SpringBootConfiguration: Acts as a **@Configuration** annotation. The annotated class becomes a bean Configuration class, indicating that the bootclass is essentially a Configuration class.

@componentScan: Automatically scans and loads qualified components, but doesn’t scan the starter dependencies we added, and eventually loads those beans into the Spring container.

@enableAutoConfiguration: This annotation enables autoconfiguration and automatically creates dependent beans.

So if we want to know how the bean was discovered ** @enableAutoConfiguration ** this annotation is important, go ahead and click on it:

@ * * * * EnableAutoConfiguration annotations introduced * * @ Import the annotations, the annotations automatic configuration needed for Import components, followed by a EnableAutoConfigurationImportSelector * * this class, Let’s go in and have a look:

EnableAutoConfigurationImportSelector inherited AutoConfigurationImportSelector class is a class selector, continue to follow AutoConfigurationImportSelector class source code:

Spring when we launch the boot framework will automatically call above this method getCandidateConfigurations means to obtain candidate configuration items from us into the jar in the package to get some configuration, The SpringFactoriesLoader class loadFactoryNames method is called from this method to keep track of the source code:

We can see that the loadFactoryNames static method of SpringFactoriesLoader reads the meta-INF /spring.factories file from all the jars we imported, and the location paths of the auto-configured classes are configured in this file:

The contents of the spring.factories file are as follows:

Then Spring Boot can load MybatisAutoConfiguration by reading the meta-INF /spring.factories file and finding the location where the configuration class was loaded. The beans are then discovered from the configuration class.

The Bean’s loading

To instantiate a normal class and hand it over to the Spring container via Spring Boot, we usually do this:

To get a normal class managed by the Spring Container in a Spring Boot application, the following methods are common:

1. Use @Configuration and @Bean annotations

Annotate this class with the @controller @Service @Repository @Component annotation and enable @ComponentScan

3. Use the @import method

Spring Boot implements auto-configuration using @import annotations, We also explained the above AutoConfigurationImportSelector class selectImports method returns a set of from the meta-inf/spring. Factories bean read from the file all the name of the class, Spring Boot can then load the beans and complete the creation of the instance.

conclusion

We can summarize the key auto-configuration steps and corresponding annotations as follows:

1. @Configuration and @Bean: Java code based Bean Configuration

2. @conditional: Sets automatic configuration Conditional dependence

3, with @ @ EnableConfigurationProperties ConfigurationProperties: reading configuration files into the bean

4. @enableAutoConfiguration and @Import: realize bean discovery and loading

The above is the exploration of the complete process of spring Boot automatic configuration starter. Later, we will further consolidate the understanding and application of automatic configuration starter through the customization of starter.