前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springboot都3.0了,自定义starter咱总不能不会吧

Springboot都3.0了,自定义starter咱总不能不会吧

作者头像
索码理
发布2024-01-15 12:24:52
4150
发布2024-01-15 12:24:52
举报
文章被收录于专栏:索码理索码理

人生的意义在于拓展,而不在于固守,别管我今天是谁,我想成为更好的自己。-- 董卿《主持人大赛》

starter是什么

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.

上面这段英文摘自spring官网,翻译过来的大致意思如下:

starter是可以包含在应用中的一组依赖描述符,借助 Spring Boot Starter,开发人员可以获得所需的所有 Spring 及相关技术的一站式服务,而无需查看示例代码或复制粘贴依赖的库文件。例如,如果需要 Spring JPA 访问数据库,则可以在工程中直接引用spring-boot-starter-data-jpa

SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。SpringBoot会自动通过classpath路径下的类发现需要的Bean,并注册进IOC容器。SpringBoot提供了针对日常企业应用研发各种场景的spring-boot-starter依赖模块。所有这些依赖模块都遵循着约定成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念。

为何要自定义starter

DRY (Don’t Repeat Yourself) 原则,通俗的说就是不重复造轮子,尽可能使用可重用的代码。我们可以把重复的代码提取出来,比如自定义一个库,然后在多个地方调用。对于相同的逻辑,在修改的时候应该只修改一个地方,而不是修改使用该相同逻辑的所有地方。尤其是使用微服务时,自定义starter是解决代码重复的关键方案。

starter命名规范

springboot 命名有两种规范:

  • spring-boot-starter-*:官方的命名规则,比如:spring-boot-starter-data-redis
  • *-spring-boot-starter:第三方项目的命名规则,比如:druid-spring-boot-starter

自定义一个starter

步骤

定义一个springboot starter分以下几步:

  1. 引入spring-boot-autoconfigure依赖
  2. 创建 配置文件XxxProperties:这个类的属性名对应着配置文件中的属性名。
  3. 创建自动配置类 AutoConfiguration:自动配置的一些逻辑,同时也要让XXXProperties 类生效。
  4. 将自动配置类放入自动配置文件中,不同版本对应的配置文件不同,按需选择。
    • springboot 2.7 之前自动配置文件为spring.factories ,配置内容的形式如下:
代码语言:javascript
复制
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
  • springboot 2.7到springboot 3.0,自动配置文件可以使用spring.factories,也可以使用META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports,推荐使用后者
  • springboot 3.0之后自动配置文件只能使用META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

实践

爱学习、爱锻炼鸡肉的人都会知道一些网站,然后通过网站各种学习知识、各种锻炼鸡肉。下面我们就做一个这样的栗子,在配置文件中配置好网站名称和链接,然后在另一个类中打来网站。

项目使用 springboot 3.2java 17

  1. 创建一个custom-starter 项目,并引入如下依赖:
代码语言:javascript
复制
<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

spring-boot-autoconfigure依赖是必须要引入的,spring-boot-configuration-processor的引入是为了在配置文件中使用属性时有提示。

  1. 创建属性配置类
代码语言:javascript
复制
@ConfigurationProperties(prefix = "movie.site")
public class MovieSiteProperties {

    /**
     * 网站名称
     */
    private String name;
    /** 网站地址 */
    private String url;
    //省去getter、setter方法
}

spring-boot-configuration-processor依赖引入之后,在属性上使用多行注释,在配置文件中使用属性时会有提示语。

  1. 创建自动配置类

创建自动配置类之前,先创建一个使用属性配置类的类

代码语言:javascript
复制
public class MovieSiteTemplate {

    private MovieSiteProperties movieSiteProperties;

    public MovieSiteTemplate(MovieSiteProperties movieSiteProperties) {
        this.movieSiteProperties = movieSiteProperties;
    }


    public void openSite(){
        System.out.println("打开网站:"+movieSiteProperties.getName()+",地址:"+movieSiteProperties.getUrl() +" 学习知识。");
    }
}

自动配置类:

代码语言:javascript
复制
@Configuration
@EnableConfigurationProperties(MovieSiteProperties.class)
public class MovieSiteAutoConfiguration {

    private  MovieSiteProperties movieSiteProperties;

    public MovieSiteAutoConfiguration(MovieSiteProperties movieSiteProperties) {
        this.movieSiteProperties = movieSiteProperties;
    }

    @Bean
    @ConditionalOnMissingBean(MovieSiteTemplate.class)
    public MovieSiteTemplate movieSiteTemplate(){
        return new MovieSiteTemplate(movieSiteProperties);
    }
}

在自动配置类中使用了条件注解 @ConditionalOnMissingBean,下一篇文件会介绍一下springboot有哪些条件注解,它们都有什么用。

  1. 创建自动配置文件 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports,引入配置类全限定名
  1. 使用Maven打包将custom-starter 打成jar包

mvn clean

  1. 在创建一个新项目springboot-practice,pom文件中引入自定义的starter 我这里是把 custom-starter 项目的artifactId改成了 movive-site-spring-boot-starter,不影响使用。
代码语言:javascript
复制
<dependency>
    <groupId>site.sunlong</groupId>
    <artifactId>movive-site-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
  1. springboot-practice项目的配置文件中配置MovieSiteProperties 类中的属性。

括号内就是MovieSiteProperties 类的属性注释,也就是上面说的提示语。

application.properties 配置文件内容,爱学习的人都知道的:

代码语言:javascript
复制
movie.site.name=91
movie.site.url=unknown
  1. 创建测试类进行测试
代码语言:javascript
复制
@SpringBootTest
class SpringbootPracticeApplicationTests {

    @Autowired
    private MovieSiteTemplate movieSiteTemplate;

    @Test
    void contextLoads() {
        movieSiteTemplate.openSite();
    }
}

打印结果

以上就是一个自定义starter的流程,操作简单,容易上手,赶紧学起来吧。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-01-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 索码理 微信公众号,前往查看

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

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

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