SpringBoot Source Parsing-Custom Start

Posted by gavinandresen on Thu, 20 Jan 2022 16:37:11 +0100

1. SpringBoot Starter mechanism

Starter in SpringBoot is a very important mechanism that allows you to discard previously complex configurations and integrate them into starter as a whole, allowing you to introduce starter dependencies in maven.

SpringBoot automatically scans the information to be loaded and starts the corresponding default configuration. starter frees us from the processing of dependent libraries and the need to configure information.

SpringBoot automatically discovers the required beans through classpath classes and registers them in the IOC container. SpringBoot provides a spring-boot-starter dependency module for developing scenarios for everyday enterprise applications.

All of these dependent modules follow the default configuration conventionally known as Configuration and allow us to adjust these configurations, that is, follow the concept of "convention is greater than configuration".

For example, if we want to introduce redis in springboot, we need to include the following in the pom

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

This is actually a starter. In short, starter is an external project that can be introduced into the current springboot project when we need to use it.

2. Why customize starter

In our day-to-day development work, there are often configuration modules that are separate from our business, and we often put them under a specific package, then if another project needs to reuse this functionality,

It is extremely cumbersome to have to hard copy the code into another project and re-integrate it once again. If we encapsulate these power configuration modules as starter s that are independent of business code,

When reusing, you just need to reference the dependency in the pom, and SpringBoot will do the automatic assembly for us, which is very easy.

3. Custom starter cases

The following are some scenarios encountered in development

_Dynamic data source.

_Login module.

_Realize log Slicing Based on AOP technology.

.........

4. Custom starter naming rules

The starter provided by SpringBoot is named after spring-boot-starter-xxx.

The official recommendation for custom starters is to use the xxx-spring-boot-starter naming rules. To distinguish between starters provided by SpringBoot ecology

5. Custom starter code implementation

The whole process is divided into two parts:

Custom starter

Use starter

5.1 Custom starter

First, finish customizing starter

5.1.1 New maven project

 

5.1.2 Import Dependency

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>
    </dependencies>

5.1.3 Writing JavaBean s

@Data
@EnableConfigurationProperties
@ConfigurationProperties("simplebean")
public class SimpleBean {

    private int id;

    private String name;
}

5.1.4 Writing configuration class MyAutoConfiguration

@Configuration
public class MyAutoConfiguration {
    static {
        System.out.println("MyAutoConfiguration init....");
    }
    @Bean
    public SimpleBean simpleBean(){
        return new SimpleBean();
    }
}

Create/META-INF/spring under 5.1.5 resources. Factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.best.config.MyAutoConfiguration

This means that when SpringBoot starts, it loads our simpleBean into the IOC container. This is actually a distorted SPI mechanism

5.2 Use custom starter

5.2.1 Import Dependency

        <dependency>
            <groupId>com.best</groupId>
            <artifactId>best-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

5.2.2 Configuring attribute values in the global profile

5.2.3 Write test methods

But here's another question, if one day we don't want to start the project and automatically assemble SimpleBean? Possible
The classmates think, that's easy. Let's go to the pom and comment out the dependencies. It's really a solution, but not a little Low.

6. Hot plug technology