From: juejin. Cn/post / 684490…

An overview of the

SpringBoot provides us with automatic assembly functions, simple and convenient. You can assemble components as you would a plug-in. Simply introduce the defined starter. It is similar to Java’s SPI mechanism, except that the SPI mechanism is designed to solve the decoupling between projects, while the starter approach implements modular full decoupling and hot plug functionality.

Customize an automated assembly implementation today, the custom starter.

Custom starter

Start by defining a configuration class module:

/**
 * Created by zhangshukang on 2018/9/20.
 */

@Configuration
@ConditionalOnProperty(name = "enabled.autoConfituration", matchIfMissing = true)
public class MyAutoConfiguration {

    static {
        System.out.println("myAutoConfiguration init...");
    }

    @Bean
    public SimpleBean simpleBean() {returnnew SimpleBean(); }} Copy the codeCopy the code

Then define a starter module that doesn’t need any code or poM dependencies. Just create a Spring. factories file under meta-INF and add the following configuration:

Org. Springframework. Boot. Autoconfigure. EnableAutoConfiguration = \ spring study. Startup. Beans. MyAutoConfiguration duplicate codeCopy the code

As shown in the figure:

Finally, we just need to introduce our starter module in the starter class project.

The principle of

In SpringBoot2 | @ SpringBootApplication annotation automation configuration process source code analysis (3), we mentioned the logic of automation configuration, method of entry for spring refresh container:

public void refresh() throws BeansException, IllegalStateException { //... invokeBeanFactoryPostProcessors(beanFactory); / /... } Duplicate codeCopy the code

Finally in AutoConfigurationImportSelector parsing spring. Factories files:

spring.factoriesDefault configuration class. As follows:

SpringBoot provides us with over 180 configuration classes, but we won’t be able to introduce them all. So when you autowire, you go to the classPath to see if there is a configuration class. If there is a configuration class, the relevant annotations such as @conditional or @conditionalonProperty should be used to judge whether the assembly is necessary. If there is no corresponding bytecode under the classPath, no processing is done. Our custom configuration classes are also assembled using the same logic, specifying the following annotations:

@ConditionalOnProperty(name = "enabled.autoConfituration", matchIfMissing = true) Copy codeCopy the code

The default is true, so the custom starter executes successfully.

SpringBoot provides a series of conditional annotations:

ConditionalOnBean (a Bean is instantiated only if an object exists in the current context) @conditionalonClass (a class is on the classpath, ConditionalOnExpression (ConditionalOnMissingBean (ConditionalOnMissingBean) @conditionalonmissingBean (ConditionalOnMissingBean)

@ ConditionalOnMissingClass (a class class path does not exist, would instantiate a Bean)

@ ConditionalOnNotWebApplication (not a web application)

Conditional annotation source code analysis will follow.