如何编写自己的 Springboot starter

编写自己的starter步骤

  1. 创建名字为xxx-spring-boot-starter 的启动器项目
  2. 创建名字为xxx-spring-boot-autoconfigure的项目
  • 编写属性绑定类xxxProperties
  • 编写服务类,引入xxxProperties
  • 编写自动配置类XXXAutoConfiguration注入配置
  • 创建spring.factories文件,用于指定要自动配置的类
  1. 启动器项目为空项目,用来引入xxx-spring-boot-autoconfigure等其他依赖
  2. 项目引入starter,配置需要配置的信息

创建启动器项目

由于启动器不需要代码实现,只需要依赖其他项目,所以直接创建一个空的 maven 项目。但是名字要规范。
这里创建的 startermyapp-spring-boot-starter
在这里插入图片描述myapp-spring-boot-starter

pom 文件非常简单,只需要引入接下来要创建的 myapp-spring-boot-autoconfigure

<?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>net.codingme.starter</groupId>
    <artifactId>myapp-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 启动器 -->
    <dependencies>
        <!--  引入自动配置项目 -->
        <dependency>
            <groupId>net.codingme.starter</groupId>
            <artifactId>myapp-spring-boot-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

创建自动配置项目

结合上面对 starter 的分析,直接创建一个名字为 myapp-spring-boot-autoconfigure 的项目。项目中只引入 springboot 父项目以及 spring-boot-starter

<?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.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>net.codingme.starter</groupId>
    <artifactId>myapp-spring-boot-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>myapp-spring-boot-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>

项目的总体结构看图
在这里插入图片描述myapp-spring-boot-starter-autoconfigure

HelloProperties中通过注解 @ConfigurationProperties(prefix = "myapp.hello")让类中的属性与 myapp.hello开头的配置进行绑定

/**
 * <p>
 *
 * @Author niujinpeng
 * @Date 2019/10/29 23:51
 */
@ConfigurationProperties(prefix = "myapp.hello")
public class HelloProperties {

    private String suffix;

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

然后在 HelloService中的 sayHello方法使用 HelloProperties 中自动绑定的值

public class HelloService {
    HelloProperties helloProperties;

    public String sayHello(String name) {
        return "Hello " + name + "," + helloProperties.getSuffix();
    }

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

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

为了让 HelloService 可以自动注入且能正常使用 HelloProperties,所以我们在
HelloServiceAutoConfiguration 类中把 HelloProperties.class 引入,然后把 HelloService 注入到 Bean

/**
 * web应用才生效
 */
@ConditionalOnWebApplication
/**
 * 让属性文件生效
 */
@EnableConfigurationProperties(HelloProperties.class)
/***
 * 声明是一个配置类
 */
@Configuration
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

最后在 spring.factories中只需要指定要自动配置的类即可

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
net.codingme.starter.HelloServiceAutoConfiguration

到这里,自动配置项目就完成了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值