avatar

目录
编写一个start启动器

前言

了解了一些SpringBoot的多环境配置、自动配置等原理,为了加深印象和理解,我们自己写一个start启动器

步骤

  1. 构建两个项目,一个是start,autoconfigure,(Maven、SpringBoot模块都行,名字自定,但最好以xxx-spring-boot-starter、xxx-spring-boot-starter-autoconfigure,xxx为你喜欢的名字);

  2. 编写两个项目的pom.xml

    xml
    <?xml version="1.0" encoding="UTF-8"?>
    <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">
    <parent>
    <artifactId>springdatademo</artifactId>
    <groupId>com.dakuzai</groupId>
    <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dakuzai-spring-boot-starter</artifactId>
    <!-- 在自己的启动器中导入自己编写的自动配置类-->
    <dependencies>
    <dependency>
    <groupId>com.dakuzai</groupId>
    <artifactId>dakuzai-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>
    </dependencies>
    </project>
    xml
    <?xml version="1.0" encoding="UTF-8"?>
    <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.0</modelVersion>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dakuzai</groupId>
    <artifactId>dakuzai-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dakuzai-spring-boot-starter-autoconfigure</name>
    <description>Demo project for Spring Boot</description>

    <properties>
    <java.version>1.8</java.version>
    </properties>

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

    </project>
  3. 具体业务

    java
    package com.dakuzai.service;

    import com.dakuzai.properties.HelloProperties;

    /**
    * @program: springdatademo
    * @description: 具体业务
    * @author: dakuzai
    * @create: 2020-03-18 16:05
    */
    public class HelloService {
    //注入配置类HelloProperties
    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
    return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
    this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
    return helloProperties.getPrefix()+name+helloProperties.getSuffix();
    }
    }
  4. xxxproperties

    java
    package com.dakuzai.properties;

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;

    /**
    * @program: springdatademo
    * @description: 封装配置文件的相关属性
    * @author: dakuzai
    * @create: 2020-03-18 16:03
    */
    //关联配置文件
    @Component
    @ConfigurationProperties(prefix = "dakuzai.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;
    }
    }
  5. xxxAutoconfigure

    java
    package com.dakuzai.configure;

    import com.dakuzai.properties.HelloProperties;
    import com.dakuzai.service.HelloService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
    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;

    /**
    * @program: springdatademo
    * @description: 自动配置类
    * @author: dakuzai
    * @create: 2020-03-18 16:26
    */
    @Configuration
    @ConditionalOnWebApplication//只有在web项目下才会生效
    @EnableConfigurationProperties(HelloProperties.class)
    public class HelloServiceAutoconfiguration {
    @Autowired
    HelloProperties helloProperties;
    @Bean
    public HelloService helloService(){
    HelloService helloService = new HelloService();
    helloService.setHelloProperties(helloProperties);
    return helloService;
    }
    }
  6. 将启动类放到META-INF/spring.factories

    Code
    # Auto Configure
    # HelloServiceAutoconfiguration 你的配置类
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.dakuzai.configure.HelloServiceAutoconfiguration
  7. 将自己编写的自动配置安装到自己的Maven仓库(两个项目都要install)
    install-spring-boot-autoconfigure

  8. 新建一个项目,如下图,在pom.xml中导入自己的启动器,再写一个controller测试即可。
    test-spring-boot
    success-spring-boot

文章作者: minghui
文章链接: https://dakuzai.ml/2020/03/18/%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E5%90%AF%E5%8A%A8%E5%99%A8/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Dakuzai’s Blog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论