Spring Boot 自定义starter启动器

Spring Boot 自定义starter启动器

命名规范

官方命名空间

  • 前缀:spring-boot-starter-
  • 模式:spring-boot-starter-模块名
  • 举例:spring-boot-starter-web、spring-boot-starter-jdbc

自定义命名空间

  • 后缀:-spring-boot-starter
  • 模式:模块-spring-boot-starter
  • 举例:mybatis-spring-boot-starter

自定义starter

创建项目maven项目zysheep-spring-boot-starter,自定义自定义启动器
Spring Boot 自定义starter启动器

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.zysheep</groupId>
    <artifactId>zysheep-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.4.5</version>
    </parent>

    <dependencies>

        <!--springboot基础包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--yml语法提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
    </dependencies>

</project>

自动配置类

Spring Boot 自定义starter启动器

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnWebApplication  //web应用才生效
@ConditionalOnProperty(prefix = "hello",name = "world",havingValue = "enabled")
//判断配置文件中是否存在某个配置hello.world=enabled,matchIfMissing=true 如果不存在,判断也是成立的
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

配置属性类

Spring Boot 自定义starter启动器

@ConfigurationProperties(prefix = "hello")
public class HelloProperties {

    private String username;

    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

service业务类

public class HelloService {

    HelloProperties helloProperties;

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
    public String sayHello() {
        return helloProperties.getUsername()+"=="+helloProperties.getPassword();
    }
}

spring.factories

Spring Boot 自定义starter启动器

在 resources 下创建文件夹 META-INF 并在 META-INF 下创建文件 spring.factories ,内容如下:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  cn.zysheep.config.HelloServiceAutoConfiguration

mvn clean install打包到本地仓库
Spring Boot 自定义starter启动器

测试

Spring Boot 自定义starter启动器

启动类

@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class,args);
    }
}

controller

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/say")
    public String sayHello() {
        return helloService.sayHello();
    }
}

yml

hello:
  username: zysheep
  password: 123123
  world: enabled

启动测试,访问localhost:8080/say
Spring Boot 自定义starter启动器
如果不配置hello.world=enabled,ioc容器中将找不到HelloService这个bean
Spring Boot 自定义starter启动器
启动报错
Spring Boot 自定义starter启动器
原因 ,是因为HelloServiceAutoConfiguration自动配置类中使用@ConditionalOnProperty(prefix = "hello",name = "world",havingValue = "enabled")注解的原因,其中判断配置文件中是否存在某个配置hello.world=enabled,不存在自动配置将不生效
Spring Boot 自定义starter启动器

上一篇:springboot的demo


下一篇:Springboot核心功能:高级特性、原理解析