美文网首页
springboot之自定义starter

springboot之自定义starter

作者: eliteTyc | 来源:发表于2019-12-16 14:26 被阅读0次

自定义starter

  • starter:

    1. 这个场景需要使用到的依赖是什么?
    2. 如何编写自动配置
    @Configuration //指定这个类是一个配置类
    @ConditionalOnXXX  //在指定条件下城里情况下自动配置类生效
    @AutoConfigureAfter // 指定自动配置类的顺序
    @Bean //给容器中添加组件
    
    @ConfigurationProperties // 结合相关xxxProperties类来绑定相关的配置
    @EnableConfigurationProperties //让xxxProperties生效并且加入到容器中
    
    自动配置类要能加载
    将需要启动就加载的自动配置类,配置在META-INF/spring.factories
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
    org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
    
    
    1. 模式:
      • 启动器只用来做依赖导入;
      • 专门来写一个自动配置模块;
      • 启动器依赖自动配置;别人只需要引入启动器(starter)
      • 官方启动器命名:spring-boot-starter-web
      • 自定义启动器:mybatis-spring-boot-starter
  • 示例

    1. 新建一个maven模块,启动器

      • groupid:com.elitetyc.starter
      • artifactid:test-spring-boot-starter
    2. 创建spring初始化器,使用spring initalizr

      • froup:com.elitetyc.starter
      • artifact:test-spring-boot-starter-autoconfigure
    3. 在启动器中添加自动配置模块

      • 修改pom.xml
      
      <!--启动器-->
      <dependencies>
          <!--引入自动配置模块-->
          <dependency>
              <groupId>com.elitetyc.starter</groupId>
              <artifactId>test-spring-boot-starter-autoconfigurer</artifactId>
              <version>0.0.1-SNAPSHOT</version>
          </dependency>
      </dependencies>
      
    4. 修改自动配置模块的内容

      1. 删除启动类和配置文件
      2. 移除pom.xml里面的test模块,和插件,和test文件夹(否则后面安装到仓库会报错,找不到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>
        <!--移除插件-->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
        
        保证自动配置模块中引入了starter
            <dependencies>
                <!--引入最基础的starter,所有starter的基本配置-->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter</artifactId>
                </dependency>
            </dependencies>
        
      3. 假设当前需求,传入一个name值,需要返回:hello+name+配置的字符串,新建HelloService.class
      
      public class HelloService {
      
          HelloProperties helloProperties;
      
          public HelloProperties getHelloProperties() {
              return helloProperties;
          }
      
          public void setHelloProperties(HelloProperties helloProperties) {
              this.helloProperties = helloProperties;
          }
      
          public String hello(String name) {
              return "hello " + name + " " + helloProperties.getSuffix();
          }
      }
      
      
      1. 新建配置类,可以读取配置文件信息HelloProperties.class
      @ConfigurationProperties(prefix = "elitetyc.hello")
      public class HelloProperties {
      
          private String suffix;
      
          public String getSuffix() {
              return suffix;
          }
      
          public void setSuffix(String suffix) {
              this.suffix = suffix;
          }
          
      }
      
      1. 新建自动配置类
      @Configuration
      @ConditionalOnWebApplication //web应用才生效
      @EnableConfigurationProperties(HelloProperties.class)
      public class HelloServiceAutoConfiguration {
      
          @Autowired
          HelloProperties helloProperties;
      
          @Bean
          public HelloService helloService(){
      
              HelloService service = new HelloService();
              service.setHelloProperties(helloProperties);
              return service;
      
      
          }
      }
      
      1. 在resource目录下新建META-INF/spring.factories,设置自动加载配置哪个类文件
          org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
          com.elitetyc.starter.HelloServiceAutoConfiguration (这里写全类名,有多个事,使用逗号,隔开)
      
      1. 将启动器模块,和自动配置模块install到仓库
      2. 其他应用如何使用:
        在其他应用的pom.xml文件中添加启动器模块的依赖即可
            <dependency>
                <groupId>com.elitetyc.starter</groupId>
                <artifactId>test-spring-boot-starter</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        
        添加完这个依赖,项目会自动导入自动配置模块
      3. 在其他项目中的配置文件中配置
      elitetyc.hello.suffix = 自己想设置的配置信息
      
      1. 在其他项目需要使用的地方自动注入HelloService类,即可调用HelloService的hello方法

相关文章

网友评论

      本文标题:springboot之自定义starter

      本文链接:https://www.haomeiwen.com/subject/qgfdnctx.html