springboot 1.5.10的自定义starter

冷暖自知 提交于 2020-02-26 22:59:39

一、自定义starter

  说到自定义starter(场景启动器),要先明确以下几点:

  • 一是这个场景需要使用什么依赖?
  • 二是如何编写springboot的自动配置?
@Configuration //指定这个类是一个配置类 
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效 @AutoConfigureAfter //指定自动配置类的顺序 
@Bean //给容器中添加组件 
@ConfigurationPropertie //结合相关xxxProperties类来绑定相关的配置 
@EnableConfigurationProperties //让xxxProperties生效加入到容器中 

自动配置类要能加载 将需要启动就加载的自动配置类,配置在META‐INF/spring.factories 
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ 
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
  • 三是自定义starter的模式

  启动器只用来做依赖导入,需要专门提供一个自动配置模块:启动器依赖于自动配置模块,其他项目只需要引入启动器即可。
在这里插入图片描述

命名规约(推荐使用以下命名规约):

【1】官方命名空间

– 前缀:“spring-boot-starter-” 
– 模式:spring-boot-starter-模块名
– 举例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc

【2】自定义命名空间

– 后缀:“-spring-boot-starter” 
– 模式:模块-spring-boot-starter
– 举例:mybatis-spring-boot-starter

二、自定义starter示例

【1】创建一个空的project
在这里插入图片描述
【2】在上一步创建的空项目中创建两个Module
在这里插入图片描述
【3】配置启动器和启动器配置模块

【3.1】配置启动器配置模块

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();
    }
}

该自定义starter可以设置的属性类:

//相当于写了一个属性,用来绑定"zm.hello"的配置
@ConfigurationProperties(prefix = "zm.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;
    }
}

使属性类生效的自动配置类:

@Configuration  //指明这是一个配置类
@ConditionalOnWebApplication  //在web应用中才生效
@EnableConfigurationProperties(HelloProperties.class)  //导入属性配置文件
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean  //将HelloService加入到容器中
    public HelloService helloService(){
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}

resources的目录下创建META-INF/spring.factories注册自动配置类:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zm.starter.springboot.HelloServiceAutoConfiguration

【3.2】配置启动器模块:在pom.xml文件中导入自动配置模块的依赖

 <!--启动器-->
 <dependencies>
     <!--引入自动配置模块-->
     <dependency>
         <groupId>com.zm.starter</groupId>
         <artifactId>springboot-starter-autoconfigure</artifactId>
         <version>0.0.1-SNAPSHOT</version>
     </dependency>
 </dependencies>

【4】创建测试项目
在这里插入图片描述
controller:

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String sayHello(){
        String sayHello = helloService.sayHello("Make");
        return sayHello;
    }
}

pom.xml文件的配置:

 <!--引入自定义的启动器-->
 <dependency>
       <groupId>com.zm.starter</groupId>
       <artifactId>springboot-zm-starter</artifactId>
       <version>1.0-SNAPSHOT</version>
 </dependency>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!