Wechat public number: Jim’s restaurant AK learn more source knowledge, welcome to pay attention to.


SpringBoot2 | SpringBoot startup process source code analysis (a)

SpringBoot2 | SpringBoot startup process source code analysis (2)

SpringBoot2 | @ SpringBootApplication annotation automation configuration process source code analysis (3)

SpringBoot2 | SpringBoot Environment analysis of source code (4)

SpringBoot2 | SpringBoot custom AutoConfiguration | SpringBoot custom starter (5)

SpringBoot2 | SpringBoot listener source code analysis | custom ApplicationListener (6)

SpringBoot2 | condition annotation @ ConditionalOnBean principle source depth resolution (7)

SpringBoot2 | Spring AOP principle source depth profiling (8)

SpringBoot2 | SpingBoot FilterRegistrationBean registered components | FilterChain responsibility chain source code analysis (9)

SpringBoot2 | BeanDefinition registered core class ImportBeanDefinitionRegistrar (10)

SpringBoot2 | Spring core extension interface | core extension methods summary (11)


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 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.bean.MyAutoConfiguration
Copy 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); / /... }Copy the code

Finally in AutoConfigurationImportSelector parsing spring. Factories files:

spring.factories

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 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, ConditionalOnMissingBean (ConditionalOnMissingBean) @conditionalonexpression (ConditionalOnMissingBean) @conditionalonmissingBean (ConditionalOnMissingBean) Will instantiate a Bean) @ ConditionalOnMissingClass (a class class path does not exist, would instantiate a Bean) @ ConditionalOnNotWebApplication (not a web application)

Conditional annotation source code analysis will follow.


SpringBoot2 | SpringBoot startup process source code analysis (a)

SpringBoot2 | SpringBoot startup process source code analysis (2)

SpringBoot2 | @ SpringBootApplication annotation automation configuration process source code analysis (3)

SpringBoot2 | SpringBoot Environment analysis of source code (4)

SpringBoot2 | SpringBoot custom AutoConfiguration | SpringBoot custom starter (5)

SpringBoot2 | SpringBoot listener source code analysis | custom ApplicationListener (6)

SpringBoot2 | condition annotation @ ConditionalOnBean principle source depth resolution (7)