spring boot 2源码系列(七)- 创建自己的starter

《Spring Boot源码博客》

使用spring-boot的时候,若需要使用一些中间件,最简单直接的方式是导入starter,例如使用redis需要做以下两步:

1、添加spring-boot-starter-data-redis依赖

2、application.properties新增redis配置。

starter极大地简化了中间件的使用,方便开发者快速开发业务功能。starter依赖于spring的自动装配功能,学习了上一篇 spring boot 2源码系列(六)- 自动装配 之后,本文教大家创建一个自己的starter。

1、创建一个工程 weather-spring-boot-starter,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.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>weather-spring-boot-starter</artifactId>
    <version>0.0.1</version>
    <name>weather-spring-boot-starter</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>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

只导入spring-boot-starter依赖即可。

2、新建WeatherProperties类,用于承载配置。

@ConfigurationProperties(prefix = "weather")
public class WeatherProperties {

    private String type = "type默认值";

    private String rate = "rate默认值";

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getRate() {
        return rate;
    }

    public void setRate(String rate) {
        this.rate = rate;
    }

}

2.1 @ConfigurationProperties(prefix = "weather")的作用是将配置文件中以weather开头的属性绑定给WeatherProperties。

2.2 WeatherProperties的属性配置了默认值,如果使用者没有在application.properties配置weather.type、weather.rate,则使用默认值。

3、新建WeatherTemplate,实现我们想要的功能。WeatherTemplate构造函数接收WeatherProperties参数,以便能使用到配置文件。

public class WeatherTemplate {

    private WeatherProperties weatherProperties;

    public WeatherTemplate(WeatherProperties weatherProperties) {
        this.weatherProperties = weatherProperties;
    }

    public void weatherPrint(){
        System.out.println(String.format("执行自定义功能,type是:%s。rate是:%s", weatherProperties.getType(), weatherProperties.getRate()));
    }

}

4、新建WeatherAutoConfiguration自动装配类

@Configuration
/**
 * @EnableConfigurationProperties的作用是使WeatherProperties配置类生效
 * @ConditionalOnProperty 配置了weather.enable=true的时候,本类生效
 */
@EnableConfigurationProperties(WeatherProperties.class)
@ConditionalOnProperty(name = "weather.enable", havingValue = "true")
public class WeatherAutoConfiguration {
    @Autowired
    private WeatherProperties weatherProperties;

    /**
     * @ConditionalOnMissingBean 如果ioc容器中没有WeatherTemplate,则本bean生效
     */
    @Bean
    @ConditionalOnMissingBean(WeatherTemplate.class)
    public WeatherTemplate weatherService(){
        return new WeatherTemplate(weatherProperties);
    }
}

5、新建resources/META-INF/spring.factories,填写如下配置,启动自动装配:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.weatherspringbootstarter.WeatherAutoConfiguration

6、将weather-spring-boot-starter打成jar包。

weather-spring-boot-starter就完成了。使用weather-spring-boot-starter与使用其他starter一样,需要引入依赖,填写配置文件。

1、在其他项目中引入weather-spring-boot-starter

<dependency>
    <groupId>com.example</groupId>
    <artifactId>weather-spring-boot-starter</artifactId>
    <version>0.0.1</version>
</dependency>

2、application.properties填写如下配置

weather.enable=true
weather.type=新类型值
#weather.rate=新比率值

3、编写一个测试类测试

@SpringBootTest
public class WeatherStarterTest {

    @Autowired
    WeatherTemplate weatherTemplate;

    @Test
    void contextLoads() {
        weatherTemplate.weatherPrint();
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值