The SpringBoot Auto Configuration process describes how to realize the automatic Configuration of the SpringBootStarter:

  1. Define the Properties configuration class

  2. Define the AutoConfiguration class

    • @configuration Annotation modification
    • @ EnableConfigurationProperties (Properties. The class) loading configuration files of the definition of the first step
    • @bean defines Bean objects that need to be managed by Spring
  3. Meta-inf /spring.factories configures automatic loading information

Step # 2 complete package of AutoConfiguration class defined path org. Springframework. Boot. Autoconfigure. EnableAutoConfiguration = \ com.***.AutoConfigurationCopy the code

1.1 Naming Rules

The starter provided by SpringBoot is named in the spring-boot-starter-xxx format. It is recommended to use the xxX-spring-boot-starter naming rule for the user-defined starter. To differentiate between SpringBoot ecology offers starter.

This section implements a simple SpringBoot Starter based on these steps. This example implements a my-spring-boot-starter package.

1. Create a Maven project

Pom files are as follows:


      
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0. RELEASE</version>
    </parent>

    <groupId>com.omg</groupId>
    <artifactId>my-spring-boot-starter</artifactId>
    <version>1.0 the SNAPSHOT</version>
    <name>my-spring-boot-starter</name>
    <description>My own implementation of SpringBootStarter</description>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

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

</project>
Copy the code

2. Implement the SpringBoot Starter

The directory structure is as follows:

2.1 Defining the Properties configuration class

package com.omg.starter.my.properties; import org.springframework.boot.context.properties.ConfigurationProperties; /** * @description: configuration file * @author: omg */ @ConfigurationProperties( prefix = "my" ) public class MyStarterProperties { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; }}Copy the code

2.2 Defining the AutoConfiguration Class

Before defining an AutoConfiguration, define a simple Service for the AutoConfiguration to validate.

2.2.1 define Service

package com.omg.starter.my.service; import com.omg.starter.my.properties.MyStarterProperties; /** * @description: Implement a simple Service * @author: omg */ public class MyStarterService { private final MyStarterProperties properties; public MyStarterService(MyStarterProperties properties) { this.properties = properties; } public String getMyInfo(){ return String.format("my name is %s, age %s", properties.getName(), properties.getAge()); }}Copy the code

2.2.2 Defining the AutoConfiguration class

  • @ConfigurationDefine the modified class as the configuration class
  • @ConditionalOnPropertyThe conditions for defining beans to take effect are in the configuration filemy.enable=trueThe class will continue to be loaded. Otherwise, the class will not be loaded
  • @EnableConfigurationPropertiesTell SpringBoot to load the configuration class, MyStarterProperties, as defined in Section 2.1.
package com.omg.starter.my.properties; import com.omg.starter.my.service.MyStarterService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @description: Spring Boot Auto Configuration * @author: omg */ @Configuration @ConditionalOnProperty(prefix = "my", name = "enable", havingValue = "true") @EnableConfigurationProperties(MyStarterProperties.class) public class MyStarterAutoConfiguration { @Autowired private MyStarterProperties properties; @Bean public MyStarterService myStarterService(){ return new MyStarterService(properties); }}Copy the code

2.3 Meta-INF/Spring. factories configure automatic loading information

Spring. The factories need to automatically load items defined SpringBoot startup, in this case, the MyStarterAutoConfiguration.

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.omg.starter.my.properties.MyStarterAutoConfiguration
Copy the code

At this point, a complete SpingBoot Starer has been implemented, followed by the verification link.

3. Verify

  1. Create a SpringBoot project and introduce the my-spring-boot-starter implemented above
< < the dependency > < groupId > com. Omg/groupId > < artifactId > my - spring - the boot - starter < / artifactId > < version > 1.0 - the SNAPSHOT < / version > </dependency>Copy the code
  1. The configuration file
my.enable=true
my.name=omg
my.age=18
Copy the code
  1. Implement Controller validation: Introduce MyStarterService in Controller.
package com.omg.test.controller; import com.omg.starter.my.service.MyStarterService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyStarterTestController { @Autowired MyStarterService myStarterService; @GetMapping("info") public String myInfo(){ return myStarterService.getMyInfo(); }}Copy the code
  1. validation