springboot之自定义starter

自定义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
      
      
      
          
          
              com.elitetyc.starter
              test-spring-boot-starter-autoconfigurer
              0.0.1-SNAPSHOT
          
      
      
    4. 修改自动配置模块的内容

      1. 删除启动类和配置文件
      2. 移除pom.xml里面的test模块,和插件,和test文件夹(否则后面安装到仓库会报错,找不到test依赖)
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
        
        保证自动配置模块中引入了starter
            
                
                
                    org.springframework.boot
                    spring-boot-starter
                
            
        
      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文件中添加启动器模块的依赖即可
            
                com.elitetyc.starter
                test-spring-boot-starter
                1.0-SNAPSHOT
            
        
        添加完这个依赖,项目会自动导入自动配置模块
      3. 在其他项目中的配置文件中配置
      elitetyc.hello.suffix = 自己想设置的配置信息
      
      1. 在其他项目需要使用的地方自动注入HelloService类,即可调用HelloService的hello方法

你可能感兴趣的:(springboot之自定义starter)