SpringBoot自定义starter

2020-03-11  本文已影响0人  CodePandaes

为什么需要自制启动器?
因为极大的提高了代码的高可用,比如在我们的日常开发工作中,如果一个工程需要复用一些独立于业务之外的配置模块时候,我们将这些可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可

搭建工程

  1. 新建一个空工程
命名为spring-boot-08
  1. 创建一个普通的maven工程hello-spring-boot-starter,这个工程里面不写业务逻辑,只是一个空的JAR文件用来依赖引入hello-spring-boot-autoconfig(下一步创建),而对外在测试类中需要引用我们自定义的启动器,只需引入hello-spring-boot-starter即可。
  1. 建立一个实现业务逻辑的工程hello-spring-boot-autoconfig,这种的命名方式是因为SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的,而官方建议自定义的starter使用xxx-spring-boot-starter命名规则,以区分SpringBoot生态提供的starter。
使用Spring Initializr快速构建

pom.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.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hello.starter</groupId>
    <artifactId>hello-spring-boot-starter-autoconfig</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-spring-boot-starter-autoconfig</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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在hello-spring-boot-starter工程的pom.xml中引入hello-spring-boot-autoconfig依赖

<?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">
    <modelVersion>4.0.0</modelVersion>

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

    <dependencies>
        <!--启动器里面只做依赖引入,引入hello-spring-boot-autoconfig-->
        <!--别人要用我们自定义的启动器,只需要引入hello-spring-boot-starter-->
        <dependency>
            <groupId>com.hello.starter</groupId>
            <artifactId>hello-spring-boot-starter-autoconfig</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

开始编码

删掉主程序和配置文件还有test目录

在hello-spring-boot-starter-autoconfig项目下新建HelloProperties实体类映射配置信息,用@ConfigurationProperties注解可以完成将application.yml配置文件内的有规则的配置参数映射到实体内的field内

@ConfigurationProperties(prefix = "hello.starter")
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;
    }
}

添加service类,通过sayHello方法完成对helloProperties的属性读取

public class HelloService {

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

添加配置类HelloServiceAutoConfiguration,如果你用来测试的工程是web工程,那么在测试工程启动的时候会自动读取此配置类

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

resources目录下新建META-INF文件夹,然后创建spring.factories文件,添加如下代码,读取配置类HelloServiceAutoConfiguration

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hello.starter.HelloServiceAutoConfiguration

安装

因为hello-spring-boot-starter依赖于hello-spring-boot-autoconfig所以先安装xxx-autoconfig,再安装xxx-starter

安装之前,记得删除pom文件中的

<!--删除,因为前面已经删除了test类,不需要了-->
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <!--记得删除,不然打包不成功-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

测试

新建一个用于测试的web工程hello-spring-boot-08-test,记得选中web组件

在pom.xml中引入启动器依赖

<!--引入自定义的starter:hello-spring-boot-starter-->
        <dependency>
            <groupId>com.hello.starter</groupId>
            <artifactId>hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

打开配置文件application,添加前缀prefix,和后缀suffix

hello.starter.prefix=2020
hello.starter.suffix=come on

添加HelloController测试

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello() {
       return helloService.sayHello("中国");
    }
}

启动项目,浏览器输入localhost:8080/hello,返回结果,自制starter测试成功

上一篇 下一篇

猜你喜欢

热点阅读