【SpringBoot新手篇】Spring Boot利用SPI思想 自定义starter启动器

【SpringBoot新手篇】Spring Boot利用SPI思想 自定义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,自定义自定义启动器
在这里插入图片描述

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>

自动配置类

在这里插入图片描述

@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;
    }
}

配置属性类

在这里插入图片描述

@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

在这里插入图片描述

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

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

mvn clean install打包到本地仓库
在这里插入图片描述

测试

在这里插入图片描述

启动类

@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
在这里插入图片描述
如果不配置hello.world=enabled,ioc容器中将找不到HelloService这个bean
在这里插入图片描述
启动报错
在这里插入图片描述
原因 ,是因为HelloServiceAutoConfiguration自动配置类中使用@ConditionalOnProperty(prefix = "hello",name = "world",havingValue = "enabled")注解的原因,其中判断配置文件中是否存在某个配置hello.world=enabled,不存在自动配置将不生效
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李熠漾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值