SpringBoot automatic configuration: customize a starter Statuser

Create a custom Starter:

The emergence of Springboot greatly simplifies the configuration of developers, and one of the great tools is the starter of springboot, which is the core component of springboot. The official springboot also encapsulates various conveniences for developers. Starter module used, for example:

  • spring-boot-starter-web//spring MVC相关
  • spring-boot-starter-aop //Aspect programming related
  • spring-boot-starter-cache //Cache related

(1) Customized Starter requirements: The
official website requires the creation of two modules, one is an autoconfigure module and the other is a starter module. The starter module depends on the autoconfigure module, which mainly serves as a transitive dependency (can be omitted).

(2) Naming convention: The
official starter spring-boot-starter-xxxis named in the format, and the starter customized by third-party developers xxxx-spring-boot-starteris named in the rule.

1 Create an autoconfiguration module

First create an empty project, and then create a SpringBootmodule, delete the startup class and test class in this class.
Insert picture description here

(1) Pom configuration

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


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

Only these two dependencies are kept in the pom file, among which spring-boot-starterare the basic dependencies of the launcher, which spring-boot-configuration-processorcan be prompted when configuring the file:
Insert picture description here

(2) Write configuration

package com.glp.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "diy.hello")
public class Properties {
    
    
    private String pre;
    private String suf;

    public String getPre() {
    
    
        return pre;
    }

    public void setPre(String pre) {
    
    
        this.pre = pre;
    }

    public String getSuf() {
    
    
        return suf;
    }

    public void setSuf(String suf) {
    
    
        this.suf = suf;
    }
}

@ConfigurationProperties: This configuration class is application.properties/application.yamlassociated with the configuration file in SpringBoot for property injection.

(3) Writing service

package com.glp.service;

import com.glp.properties.Properties;

public class HelloService {
    
    

    Properties properties;

    public Properties getProperties() {
    
    
        return properties;
    }

    public void setProperties(Properties properties) {
    
    
        this.properties = properties;
    }
    public String sayHello(){
    
    
        return properties.getPre()+"offer"+properties.getSuf();
    }
}

(4) Automatic configuration
An integration of the service and the configuration class in the automatic configuration class is equivalent to passing the configured class into the service class.

package com.glp.config;

import com.glp.properties.Properties;
import com.glp.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(Properties.class)
public class HelloServiceAutoConfiguration {
    
    
    @Autowired
    Properties properties;

    @Bean
    public HelloService getHelloService(){
    
    
        HelloService service = new HelloService();
        service.setProperties(properties);
        return service;
    }
}
  • @ConditionalOnXXXX: Used to determine whether certain conditions are met, and to determine whether the automatic configuration takes effect
  • @Bean: Integrate services and configuration classes, configure them into components, and inject them into the container for users to call.
  • @EnableConfigurationProperties : Let xxxProperties take effect and add it to the container

(5)spring.factories

Write your own META-INFfile. When the project starts to scan the package, the configuration class specified under the spring.factories file will be automatically loaded.

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.glp.config.HelloServiceAutoConfiguration

2 Create a starter statuser module

This module is only used for transitive dependencies, so it is Mavenenough for us to create a new module.
Insert picture description here

Only need to introduce our custom in the pom autoconfigure, it plays a dependent role. Of course if you don’t want toAutomatic configuration code and dependency management Separately, you can also use only one module.

    <dependencies>
        <dependency>
            <groupId>com.glp</groupId>
            <artifactId>diy-spring-boot-autoconfiguration</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

Note: To
introduce the automatic configuration class, just copy the groupId, artifactId and version of the automatic configuration class pom.xml.
Insert picture description here

3 Install the automatic configuration module and startup module into the Maven warehouse

Insert picture description here

4 Writing test classes

Let's create a new SpringBootmodule test class, and just introduce the Web module into the test class, without introducing too much.
Insert picture description here

(1) Introduce dependency in the test class:

     <dependency>
            <groupId>com.glp</groupId>
            <artifactId>diy-spring-boot-stater</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

(2) Applicantrion.properties configuration

diy.hello.pre=where
diy.hello.suf=offer

(3) Write a controller:

package com.glp.controller;


import com.glp.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    
    
    @Autowired
    HelloService helloService;

    @GetMapping("/say")
    public String helloController(){
    
    
        return helloService.sayHello();
    }
}

(4) Operation results

Insert picture description here

Guess you like

Origin blog.csdn.net/glpghz/article/details/108427941