自定义SpringBootStarter


自定义SpringBootStarter

说明

启动器模块是一个 空 jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;

  • 命名归约:

    • 官方命名:

      • 前缀:spring-boot-starter-xxx

      • 比如:spring-boot-starter-web....

    • 自定义命名:

      • 前缀:xxx-spring-boot-starter

      • 比如:mybatis-spring-boot-starter

编写启动器

  1. 在IDEA中新建一个空项目 spring-boot-starter-diy

  2. 新建一个普通Maven模块:hello-spring-boot-starter

  3. 新建一个Springboot模块:hello-spring-boot-starter-autoconfigure

  4. 点击apply即可

  5. 在的 starter模块中导入autoconfigure模块的依赖

    
        
            com.example
            hello-spring-boot-starter-autoconfigure
            0.0.1-SNAPSHOT
        
    
    
  6. 将 autoconfigure 模块下多余的文件(src/main和pom以外的文件)都删掉,pom中只留下一个 starter,这是所有的启动器基本配置

    <?xml version="1.0" encoding="UTF-8"?>
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.3.7.RELEASE
             
        
        com.example
        hello-spring-boot-starter-autoconfigure
        0.0.1-SNAPSHOT
        hello-spring-boot-starter-autoconfigure
        hello-spring-boot-starter-autoconfigure
        
            1.8
        
        
            
                org.springframework.boot
                spring-boot-starter
            
        
    
    
  7. 编写HelloProperties 配置类

    @ConfigurationProperties(prefix = "wang.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;
        }
    }
    
  8. 编写一个自己的服务

    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();
        }
    }
    
  9. 编写我们的自动配置类并注入bean,测试!

    @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;
        }
    }
    
  10. 在resources编写一个自己的 META-INF\spring.factories(自己创建文件)

    # Auto Configure
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.example.hellospringbootstarterautoconfigure.com.config.HelloServiceAutoConfiguration
    
  11. 编写完成后,可以安装到maven仓库中

    maven install (idea可点击侧边 maven 的 install 按钮)

    注意是hello-spring-boot-starter项目进行此操作

测试

新建项目测试我们自己的写的启动器

  1. 新建一个SpringBoot 项目spring-boot-start-diy-test

  2. 导入我们自己写的启动器

    
    
        org.example
        hello-spring-boot-starter
        1.0-SNAPSHOT
    
    
  3. 编写一个 HelloController 进行测试我们自己的写的接口!

    @RestController
    public class HelloController {
        @Autowired
        HelloService helloService;
    
        @RequestMapping("/hello")
        public String hello(){
            return helloService.sayHello("?");
        }
    }
    
  4. 编写配置文件 application.properties

    wang.hello.prefix=WJ
    wang.hello.suffix=WY
    
  5. 启动项目进行测试,结果成功

    GET http://localhost:8080/hello
    
    WJ?WY