Springboot series — automatic assembly principle 2

Time:2021-9-20

The powerful feature of springboot is that it extracts common scenes into scene starters. Through these scene starters and our customized configuration, we can use relevant functions. However, in some cases, these scenario initiators cannot meet our needs and need to be customized.

Let’s take a look at the scenario launcher encapsulated by springboot

Springboot series -- automatic assembly principle 2

As you can see, in the mybatis spring boot starter scenario, there is no code

Springboot series -- automatic assembly principle 2

In his POM file, we found that it actually relies on a mybatis spring boot autoconfigure

Springboot series -- automatic assembly principle 2

In this mybatis spring boot autoconfigure, we see the function code related to mybatis

So we conclude:

  • The starter is an empty jar file that only provides auxiliary dependency management. These dependencies may be used for automatic assembly or other class libraries.
  • You need to write a configuration module similar to spring boot autoconfigure
  • When using, you only need to introduce the starter, and you can use automatic configuration

How to customize springbootstart

Custom start step process

1. Create a parent Maven project

Project Name: springboot customer start

Springboot series -- automatic assembly principle 2

Maven configuration

Springboot series -- automatic assembly principle 2

1. Kuroko spring boot starter configuration

<? xml version="1.0" encoding="UTF-8"?>

2. Kuroko spring boot autoconfigure configuration

<? xml version="1.0" encoding="UTF-8"?>

3、HelloAutoConfitguration

/***

4、HelloProperties

@ConfigurationProperties("kuroko.hello")
public class HelloProperties {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

5、IndexController

@RestController

6、spring.factories

Create the folder meta-inf under resources and the file spring.factories under meta-inf, as follows:

Springboot series -- automatic assembly principle 2

Springboot series -- automatic assembly principle 2

Install the configured Kuroko spring boot starter and Kuroko spring boot autoconfigure into local jar packages

Springboot series -- automatic assembly principle 2

Create the test class test springboot start

Springboot series -- automatic assembly principle 2

Introduce a custom startup class: Kuroko spring boot starter

Springboot series -- automatic assembly principle 2

Configuration profile

Done!!!

This work adoptsCC agreement, reprint must indicate the author and the link to this article