SpringBoot Starter 分析及编写自己的Starter

SpringBoot Starter 分析

一、SpringBoot Starter作用

在不使用springboot开发项目的时候,或者说springboot还没有出来的时候。我们要在项目中引用某一个功能,比如redis。

  1. 首先你得去下载一个redis连接的jar包,如jedis,选择好版本。
  2. 接着你还要去下载一个和上面jedis版本对应的spring-data-redis。版本得对应上,不然会出现很多奇怪的错误。
  3. 在spring的applicationContext.xml文件中配置redis相关的信息。

在一个完整的项目中,还会引入很多其他的jar包,要和spring进行整合。每一种jar包都要把上面的步骤重复一般,是很繁琐的。
springboot的中的starter机制就是为了解决上述问题的。

二、认识starter

我们先来看看starter长什么样

  1. spring官方的starter
    在这里插入图片描述
    spring官方的starter的命名是spring-boot-starter-xxx,注意看上面截图中jar包里面没有代码,也没有pom.xml文件。那这些jar用来干啥呢。
    了解过springboot自动配置原理应该知道,它的自动配置功能全部是放在spring-boot-autoconfigure这个jar包中的。这可能也是它的约定优于配置原则的体现。
    所以使用官方starter的流程是依赖spring-boot-starter-parent 这个包,spring-boot-starter-parent又依赖spring-boot-dependencies,然后spring-boot-dependencies这个包里面包含了spring-boot-autoconfigure。
  2. 第三方的starter
    再看看我们经常使用的mybatis的starter
    在这里插入图片描述
    里面也没有代码,但是有一个pom.xml文件,看看里面的依赖
    在这里插入图片描述里面有一个mybatis-spring-boot-autoconfigure的包。看看这个包里面的内容
    在这里插入图片描述
    这个包里面有代码,mybatis-spring-boot-starter的功能就是在这里面实现的。我们来分析下这里面的文件
    • pom.properties 配置maven所需的项目version、groupId和artifactId
    • pom.xml 配置所依赖的jar包
    • additional-spring-configuration-metadata.json 手动添加IDE提示功能
    • MANIFEST.MF 这个文件描述了该Jar文件的很多信息
    • spring.factories 自动配置要读取的文件
    • spring-autoconfigure-metadata.properties pom文件中引入spring-boot-autoconfigure-processor这个jar包后自动生成的文件
    • spring-configuration-metadata.json 在pom文件中引入了spring-boot-configuration-processor这个jar包后自动生成的IDE提示功能文件,和上面的additional-spring-configuration-metadata.json作用一样,最终会合并为一个。在yml配置文件中有提示功能。
    • ConfigurationCustomizer 自定义Configuration回调接口
    • MybatisAutoConfiguration 配置类
    • MybatisProperties 属性类
    • SpringBootVFS 扫描嵌套的jar包中的类

重点看看MybatisProperties、MybatisAutoConfiguration、spring.factories这三个文件

  • MybatisProperties属性类
    在这里插入图片描述
    mybatis提供出来的配置全部定义在这里。
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
public class MybatisProperties {

  public static final String MYBATIS_PREFIX = "mybatis";

从上面这几行代码可以看出,mybatis的配置是以 mybatis开始的,大概向下面这样
在这里插入图片描述

  • MybatisAutoConfiguration 配置类
    在这里插入图片描述
    在这里插入图片描述

上面截图只截了一部分,类上面的注解决定这个配置类是否生效。最重要的是这个配置类中生成了我们经常使用的SqlSessionFactory这个Bean。

  • spring.factories
    在这里插入图片描述
    这个starter的jar包被引入到SpringBoot项目中,项目启动时,会来读取这个文件,完成自动装配和属性的自动配置。
    我们总结一下上面介绍的mybatis-spring-boot-starter这个starter的内容。
  • 以第三方的starter包的命名规则创建一个mybatis-spring-boot-starter的空项目,里面只有pom.xml和pom.properties文件。
  • pom.xml文件中包含mybatis-spring-boot-autoconfigure项目
  • mybatis-spring-boot-autoconfigure 项目中包含MybatisAutoConfiguration配置类,这个配置类中定义了一些给外部使用的Bean,并且该配置类根据一些条件注解来判断是否生效。该项目中还有一个MybatisProperties属性类,给使用者提供自定义的配置。
  • 在META-INF文件夹中编写spring.factories文件,key为org.springframework.boot.autoconfigure.EnableAutoConfiguration,value为org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
  1. 另外一种形式的第三方Starter
    再来看一下alibaba的数据源Druid的Starter
    在这里插入图片描述
    这个Starter里面包含了实现代码,没有autoconfigure项目。两种形式的第三方Starter功能都是能实现的。

三、编写自己的Starter

我们参照mybatis-spring-boot-starter的实现步骤编写一个Starter。该Starter封装了多个消息发送渠道,业务模块只需引入该starter并配置好使用的渠道就可实现消息发送功能。

首先创建一个工程,包含三个子模块 example-spring-boot-starter、example-spring-boot-autoconfigure、example-spring-boot-test,目录结构如下:
在这里插入图片描述

example-spring-boot-autoconfigure

先来看看 example-spring-boot-autoconfigure ,这是功能实现的模块
ExampleProperties类

@ConfigurationProperties(prefix = ExampleProperties.EXAMPLE_PREFIX)
public class ExampleProperties {
    public static final String EXAMPLE_PREFIX = "example";

    private Integer type;

    public Integer getType() {
        return type;
    }

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

这个类提供了使用者可配置的属性。我们这里就一个简单的type,及消息发送渠道类型。

ExampleMessageSendService类

public class ExampleMessageSendService {

    private static Map<Integer,String> messageMap = new HashMap<>();

    static {
        messageMap.put(1,"短信消息");
        messageMap.put(2,"微信公众号消息");
        messageMap.put(3,"邮件消息");
    }

    private Integer type;

    public ExampleMessageSendService(Integer type){
        this.type = type;
    }

    public void sendMsg(){
        if(!messageMap.containsKey(type)){
            System.out.println("当前消息渠道未实现");
            return;
        }
        System.out.println("发送" + messageMap.get(type) + "成功");
    }
}

这个类是具体的功能逻辑实现类,根据使用者的配置信息选择消息渠道发送消息。注意,这个类只是一个普通的类,没有使用@Service等注解。是在配置类中被加载为Bean的。

ExampleAutoConfigure类

@ConditionalOnClass(ExampleProperties.class)
@EnableConfigurationProperties(ExampleProperties.class)
@Configuration
public class ExampleAutoConfigure {

    @Autowired
    private ExampleProperties properties;

    @Bean
    public ExampleMessageSendService exampleMessageSendService(){
        return new ExampleMessageSendService(properties.getType());
    }
}

这个类是配置类,自动配置功能会扫描到它。它的主要功能就是生成功能实现类的Bean,并将该Bean和属性配置类联系起来。

spring.factories 文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autoconfigure.ExampleAutoConfigure

这个文件指定 ExampleAutoConfigure 类是需要被扫描的配置类。

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>example</artifactId>
        <groupId>springboot-starter-example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>example-spring-boot-autoconfigure</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
    </dependencies>
</project>

example-spring-boot-starter

example-spring-boot-starter模块中没有功能实现,只在pom文件中引入了example-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">
    <parent>
        <artifactId>example</artifactId>
        <groupId>springboot-starter-example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>example-spring-boot-starter</artifactId>

    <dependencies>
        <dependency>
            <groupId>springboot-starter-example</groupId>
            <artifactId>example-spring-boot-autoconfigure</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

example-spring-boot-test

测试模块。
先在pom.xml文件中引入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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>example</artifactId>
        <groupId>springboot-starter-example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>example-spring-boot-test</artifactId>

    <dependencies>
        <dependency>
            <groupId>springboot-starter-example</groupId>
            <artifactId>example-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Application.yml配置

example:
  type: 1

Application启动类:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

TestRuuner类,在项目启动后验证功能

@Component
public class TestRuuner implements ApplicationRunner {

    @Autowired
    private ExampleMessageSendService exampleMessageSendService;

    public void run(ApplicationArguments args) throws Exception {
        exampleMessageSendService.sendMsg();
    }
}

以上,就是SpringBoot Starter的分析以及自己编写Starter的实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值