前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何定制化Spring Boot Starter,这次我终于学会了

如何定制化Spring Boot Starter,这次我终于学会了

作者头像
百思不得小赵
发布2022-12-27 16:53:15
3530
发布2022-12-27 16:53:15
举报
文章被收录于专栏:小赵Java总结小赵Java总结

文章目录

Spring Boot Starter官网描述:Spring Boot Starter官方介绍

什么是Spring Boot Starter

Starters可以理解为启动器,它包含了一系列可以集成到应用里面的依赖包,可以一站式集成 Spring和其他技术,而不需要到处找示例代码和依赖包。Spring Boot Starter的工作原理是:Spring Boot在启动时扫描项目所依赖的JAR包,寻找包含spring.factories文件的JAR包,根据spring.factories配置加载AutoConfigure类,根据@Conditional注解的条件,进行自动配置并将Bean注入Spring Context

为什么要自定义Spring Boot Starter?

在Spring Boot官网为了简化我们的开发,已经提供了非常多场景的Starter来为我们使用,即便如此,也无法全面的满足我们实际工作中的开发场景,这时我们就需要自定义实现定制化的Starter。

image.png
image.png

实现步骤

1.首先,创建一个Maven空工程,添加两个模块

启动器

启动器中没有任何的源代码,只是告诉我们当前场景需要引入哪些依赖即可!

创建启动器模块为maven工程,命名为 xiaozhao-hello-spring-boot-starter,对应的依赖文件

代码语言:javascript
复制
<groupId>com.zhao</groupId>
<artifactId>xiaozhao-hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>

自动配置包

自动配置包中实现了所有的自动配置功能!

创建自动配置包模块为SpringBoot初始化工程,命名为xiaozhao-hello-spring-boot-starter-autoconfigure

image.png
image.png

最终的项目模块如下:

image.png
image.png

2.模块创建完成后,需要在启动器中引入自动配置模块(别人引入场景启动器,自动配置包就会自动引入)

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>com.zhao</groupId>
        <artifactId>xiaozhao-hello-spring-boot-starter-autoconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

3.编写自动配置模块。

  • 创建自定义的Properties文件
代码语言:javascript
复制
@ConfigurationProperties("xiaozhao.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;
    }
}
  • 创建业务类读取Properties文件中的值
代码语言:javascript
复制
public class HelloService {

    @Autowired
    HelloProperties helloProperties;

     public String sayHello(String userName){
        return helloProperties.getPrefix() + ":" + userName + ">" + helloProperties.getSuffix();
     }
}
  • 再个自动配置类,自动进行类加载
代码语言:javascript
复制
@Configuration
@ConditionalOnMissingBean(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        return helloService;
    }
}

最终的效果如下:

image.png
image.png

4.在resources目录下创建META-INF/spring.factories添加如下配置信息

代码语言:javascript
复制
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zhao.hello.auto.HelloServiceAutoConfiguration
image.png
image.png

5.装hello-spring-boot-starter-autoconfigure模块和xiaozhao-hello-spring-boot-starter

image.png
image.png
image.png
image.png

6.装完成后,创建新的项目来引入创建好的starter.新的的项目名为hello-test的Spring Boot初始化项目。

image.png
image.png

引入我们之前定义的Starter启动器和Spring Boot自带的Web启动器

代码语言:javascript
复制
<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>

   <dependency>
      <groupId>com.zhao</groupId>
      <artifactId>xiaozhao-hello-spring-boot-starter</artifactId>
      <version>1.0-SNAPSHOT</version>
   </dependency>
</dependencies>
image.png
image.png

7.在hello-test项目中创建测试Controller

代码语言:javascript
复制
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

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

编写配置文件

代码语言:javascript
复制
xiaozhao.hello.prefix=hello
xiaozhao.hello.suffix=666
image.png
image.png

8.启动项目,测试一下

image.png
image.png

总结自定义Starter的实现逻辑

  1. 首先引入自定义的Starter:xiaozhao-hello-spring-boot-starter,在这个Starter中引入自定义的自动配置场景
  2. 当自动配置场景启动时,会去寻找spring.factories文件,去自动加载HelloServiceAutoConfiguration类文件
  3. 加载完自动配置类后,@ConditionalOnMissingBean(HelloService.class)通过这个注解,当容器中没有HelloService时,去自动添加一个HelloService组件。
  4. HelloService组件的所有属性,通过HelloProperties配置文件进行绑定的,@ConfigurationProperties("xiaozhao.hello"),通过xiaozhao.hello。xxx进行绑定。
  5. 如果自己在容器中注入一个HelloService组件,使用的就不是自动配置的,而是重新注入的。
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-12-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 什么是Spring Boot Starter
  • 实现步骤
    • 启动器
      • 自动配置包
      • 总结自定义Starter的实现逻辑
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档