Spring Boot自定义starter

The article summary

  • 1 新建一个工程
  • 2 `xxx-sprig-boot-starter-configure`模块自动配置编码
    • 2.1 服务层编码
    • 2.2 属性配置类编码
    • starter自动配置类编码
    • 2.4 添加自动配置类到META-INF路径下
    • 2.5 将工程安装到本地
  • 3 新建一个工程测试自定义starter
    • 3.1 编写controller层
    • 3.2 编写配置文件
  • 4 测试结果
    • 4.1 使用starter默认配置
    • 4.2 使用自定义配置

1 新建一个工程

工程由xxx-sprig-boot-starterxxx-sprig-boot-starter-configure两个模块组成;
Spring Boot自定义starter_第1张图片

  • xxx-sprig-boot-starter模块
    • 只用来做依赖导入
    • 依赖于 xxx-sprig-boot-starter-configure模块,没有实际代码
    
    <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.0modelVersion>
    
        <groupId>com.andergroupId>
        <artifactId>ander-spring-boot-starterartifactId>
        <version>1.0-SNAPSHOTversion>
    
        
        <dependencies>
            <dependency>
                <groupId>com.andergroupId>
                <artifactId>ander-spring-boot-starter-configureartifactId>
                <version>0.0.1-SNAPSHOTversion>
            dependency>
        dependencies>
    
    project>
    
    Spring Boot自定义starter_第2张图片
  • xxx-sprig-boot-starter-configure模块
    • 专门自动配置模块
    • 依赖于spring-boot-starter-web
    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.3.10.RELEASEversion>
            <relativePath/> 
        parent>
    
        <groupId>com.andergroupId>
        <artifactId>ander-spring-boot-starter-configureartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>ander-spring-boot-starter-configurename>
        <description>Demo project for Spring Bootdescription>
    
        <properties>
            <java.version>1.8java.version>
        properties>
        
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
        dependencies>
    
    project>
    
    Spring Boot自定义starter_第3张图片

2 xxx-sprig-boot-starter-configure模块自动配置编码

2.1 服务层编码

/**
 * Service层
 *
 * @Author: Ander
 * @Date: 2021-05-04
 */
public class HelloService {

    private HelloServiceProperties helloServiceProperties;

    public String helloService(String name) {
        return helloServiceProperties.getPrefix() + " "+ name + " " + helloServiceProperties.getSuffix();
    }

    public HelloServiceProperties getHelloServiceProperties() {
        return helloServiceProperties;
    }

    public void setHelloServiceProperties(HelloServiceProperties helloServiceProperties) {
        this.helloServiceProperties = helloServiceProperties;
    }
}

2.2 属性配置类编码

/**
 * 属性配置类
 *
 * @Author: Ander
 * @Date: 2021-05-04
 */
@ConfigurationProperties(prefix = "com.ander")
public class HelloServiceProperties {

    private String prefix = "hi";
    private String suffix = "hello world";

    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;
    }
}

starter自动配置类编码

@EnableConfigurationProperties({HelloServiceProperties.class})作用:让xxxProperties生效加入到容器中

/**
 * 自定义starter自动配置类
 *
 * @Author: Ander
 * @Date: 2021-05-04
 */
@Configuration
@ConditionalOnWebApplication // 指定web应用才生效
@EnableConfigurationProperties({HelloServiceProperties.class})
public class HelloServiceAutoConfigure {

    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloServiceProperties(helloServiceProperties);
        return helloService;
    }
}

2.4 添加自动配置类到META-INF路径下

Spring Boot自定义starter_第4张图片

2.5 将工程安装到本地

注意先安装xxx-spring-boot-starter-configure,再安装xxx-spring-boot-starter
Spring Boot自定义starter_第5张图片

3 新建一个工程测试自定义starter

3.1 编写controller层

/**
 * starter测试控制类
 *
 * @Author: Ander
 * @Date: 2021-05-05
 */
@RestController
public class StarterTestController {

    @Autowired
    private HelloService helloService;

    @GetMapping("hello")
    public String hello(String name) {
        return helloService.helloService(name);
    }
}

3.2 编写配置文件

server.port=8888
com.ander.prefix=HI
com.ander.suffix=HELLO WORLD

4 测试结果

4.1 使用starter默认配置

Spring Boot自定义starter_第6张图片

4.2 使用自定义配置

Spring Boot自定义starter_第7张图片

你可能感兴趣的:(spring,boot)