How to customize Spring Boot Starter, I finally learned this time

image.png

Spring Boot Starter official website description : Spring Boot Starter official introduction

What is Spring Boot Starter

Starters can be understood as a starter, which contains a series of dependency packages that can be integrated into the application. It can integrate Spring and other technologies in one stop without looking for sample code and dependency packages everywhere. The working principle of Spring Boot Starter is: Spring Boot scans the JAR package that the project depends on at startup, looks for the JAR package containing the spring.factories file, spring.factoriesloads the AutoConfigureclass according to the configuration @Conditional, performs automatic configuration and injects the Bean according to the annotation conditionsSpring Context

Why customize Spring Boot Starter?

In order to simplify our development, the Spring Boot official website has provided a lot of starters for us to use. Even so, it cannot fully meet the development scenarios in our actual work. At this time, we need to customize the custom implementation Starter.

image.png

Implementation steps

1. First, create a Maven empty project and add two modules

Launcher

There is no source code in the launcher, just tell us which dependencies need to be introduced in the current scene!

Create a launcher module as a maven project, named as
xiaozhao-hello-spring-boot-starter, the corresponding dependency file

<groupId>com.zhao</groupId>
<artifactId>xiaozhao-hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>

auto-configuration package

All autoconfiguration features are implemented in the autoconfiguration package!

Create an automatic configuration package module as the SpringBoot initialization project, namedxiaozhao-hello-spring-boot-starter-autoconfigure

image.png

The final project module is as follows:

image.png

2. After the module is created, you need to import the automatic configuration module into the launcher (if someone else introduces the scene launcher, the automatic configuration package will be automatically imported)

<dependencies>
    <dependency>
        <groupId>com.zhao</groupId>
        <artifactId>xiaozhao-hello-spring-boot-starter-autoconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

3. Write the automatic configuration module.

  • Create a custom Properties file
@ConfigurationProperties("xiaozhao.hello")
public class HelloProperties {
    
    
    private String prefix;
    private String suffix;

    public String getPrefix() {
    
    
        return prefix;
    }

    public void setPrefix(String prefix) {
    
    
        this.prefix = prefix;
    }

    public String getSuffix() {
    
    
        return suffix;
    }

    public void setSuffix(String suffix) {
    
    
        this.suffix = suffix;
    }
}
  • Create a business class to read the values ​​​​in the Properties file
public class HelloService {
    
    

    @Autowired
    HelloProperties helloProperties;

     public String sayHello(String userName){
    
    
        return helloProperties.getPrefix() + ":" + userName + ">" + helloProperties.getSuffix();
     }
}
  • Another automatic configuration class, automatic class loading
@Configuration
@ConditionalOnMissingBean(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    
    
    @Bean
    public HelloService helloService(){
    
    
        HelloService helloService = new HelloService();
        return helloService;
    }
}

The final effect is as follows:

image.png

4. resourcesCreate META-INF/spring.factories in the directory and add the following configuration information

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zhao.hello.auto.HelloServiceAutoConfiguration

image.png
5. Install the hello-spring-boot-starter-autoconfiguremodule andxiaozhao-hello-spring-boot-starter

image.png

image.png

6. After the installation is complete, create a new project to introduce the created starter. The new project is called hello-testthe Spring Boot initialization project.

image.png

Introduce the Starter starter we defined earlier and the Web starter that comes with Spring Boot

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

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

   <dependency>
      <groupId>com.zhao</groupId>
      <artifactId>xiaozhao-hello-spring-boot-starter</artifactId>
      <version>1.0-SNAPSHOT</version>
   </dependency>
</dependencies>

image.png
7. hello-testCreate a test Controller in the project

@RestController
public class HelloController {
    
    

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String sayHello() {
    
    
        String str = helloService.sayHello("李四");
        return str;
    }
}

Write a configuration file

xiaozhao.hello.prefix=hello
xiaozhao.hello.suffix=666

image.png

8. Start the project and test it

image.png

Summarize the implementation logic of custom Starter

  1. First introduce a custom Starter: xiaozhao-hello-spring-boot-starter, introduce a custom automatic configuration scenario in this Starter
  2. When the automatic configuration scene starts, it will look for the spring.factories file to automatically load HelloServiceAutoConfigurationthe class file
  3. After the automatic configuration class is loaded, @ConditionalOnMissingBean(HelloService.class)through this annotation, when there is no component in the container, HelloServicea component is automatically added HelloService.
  4. HelloServiceAll properties of the component are bound through HelloPropertiethe s configuration file, @ConfigurationProperties("xiaozhao.hello"), through xiaozhao.hello。xxxbinding.
  5. If you inject a component in the container yourself HelloService, it is not automatically configured, but re-injected.

Guess you like

Origin blog.csdn.net/Zp_insist/article/details/128438990