首页 > 技术文章 > SpringBoot07-starter

binwenhome 2020-05-17 20:53 原文

简介

  1. SpringBoot为我们提供了各类的场景启动器, 全面且丰富, 并且会帮我们自动配置好. 我们只需要在配置文件中做少量配置即可.
  2. 但即使这样, 仍不能囊括开发中的所有场景, 故我们需要自定义starter

自定义starter

  • 对于一个starter来说
    1. 该场景需要使用的依赖是什么
    2. 如何编写自动配置
      @Configuration  //指定这个类是一个配置类
      @ConditionalOnXXX  //在指定条件成立的情况下自动配置类生效
      @AutoConfigureAfter  //指定自动配置类的顺序
      @Bean  //给容器中添加组件
      
      @ConfigurationPropertie(prefix ="xxx") //结合相关xxxProperties类来绑定相关的配置
      @EnableConfigurationProperties //让xxxProperties生效, 并加入到容器中
      
      自动配置类要想加载
      把需要启动就加载的自动配置类, 配置在: META-INF/spring.factories
      //比如: org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
  • 模式
    1. 启动器只用来做依赖导入.
    2. 专门写一个自动配置模块.
    3. 启动器依赖自动配置, 别人只需要引入启动器即可(starter)
    4. 命名
      1. 官方: 以spring-boot-starter- 作前缀.
      2. 自定义: 以-spring-boot-starter作后缀, 如mybatis-spring-boot-starter.
  • 自定义
    1. 创建两个模块, 启动器模块, 自动配置模块
    2. 其中, 启动器只依赖自动配置模块, 其他什么也不做.
          <!--  启动器  -->
          <dependencies>
              <!-- 引入自动配置模块 -->
              <dependency>
                  <groupId>top.binwenhome.starter</groupId>
                  <artifactId>bw-spring-boot-starter-aufoconfigurer</artifactId>
                  <version>0.0.1-SNAPSHOT</version>
              </dependency>
          </dependencies>
    3. 自动配置模块中至少要引入spring-boot-starter
          <dependencies>
              <!-- 引入spring-boot-starter, 所有starter的基本配置 -->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter</artifactId>
              </dependency>
          </dependencies>
    4. 自动配置模块编写xxxProperties
      @ConfigurationProperties(prefix = "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;
          }
      }
      • 引用该starter的工程, 我们在application.properties中书写的配置就对应这些xxxProperties
        hello.prefix=xxx
        hello.suffix=xxx
    5. 自动配置模块编写核心服务类
      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();
          }
      }
    6. 自动配置模块编写自动配置类
      @Configuration
      @ConditionalOnWebApplication //web应用才生效
      @EnableConfigurationProperties(HelloProperties.class)
      public class HelloServiceAutoConfiguration {
      
          @Autowired
          HelloProperties helloProperties;
      
          public HelloService helloService() {
              HelloService service = new HelloService();
              service.setHelloProperties(helloProperties);
              return service;
          }
      }
    7. 在resources下创建META-INF包, 包里新建spring.factories文件
      org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
      top.binwenhome.starter.HelloServiceAutoConfiguration
    8. 之后依次install这两个模块即可.

推荐阅读