Code Ease Code Ease
  • 个人博客网站 (opens new window)
  • 好用的工具网站 (opens new window)
  • Java核心基础
  • 框架的艺术
  • 分布式与微服务
  • 开发经验大全
  • 设计模式
  • 版本新特性
数据库系列
大数据+AI
  • xxl-job
运维与Linux
  • 基于SpringBoot和BootStrap的论坛网址
  • 基于VuePress的个人博客网站
  • 基于SpringBoot开发的小功能
  • 做一个自己的IDEA插件
程序人生
关于我
  • 分类
  • 标签
  • 归档

神秘的鱼仔

你会累是因为你在走上坡路
  • 个人博客网站 (opens new window)
  • 好用的工具网站 (opens new window)
  • Java核心基础
  • 框架的艺术
  • 分布式与微服务
  • 开发经验大全
  • 设计模式
  • 版本新特性
数据库系列
大数据+AI
  • xxl-job
运维与Linux
  • 基于SpringBoot和BootStrap的论坛网址
  • 基于VuePress的个人博客网站
  • 基于SpringBoot开发的小功能
  • 做一个自己的IDEA插件
程序人生
关于我
  • 分类
  • 标签
  • 归档
服务器
  • Java核心基础

  • 框架的艺术

    • Spring

    • Mybatis

    • SpringBoot

      • 如何用SpringBoot(2.3.3版本)快速搭建一个项目
      • 一步步带你看SpringBoot(2.3.3版本)自动装配原理
      • SpringBoot配置文件及自动配置原理详解,这应该是SpringBoot最大的优势了吧
      • SpringBoot整合jdbc、durid、mybatis详解,数据库的连接就是这么简单
      • SpringBoot整合SpringSecurity详解,认证授权从未如此简单
      • SpringBoot整合Shiro详解,还在自己写登陆注册早落伍了
      • SpringBoot如何实现异步、定时任务?
      • 如何在SpringBoot启动时执行初始化操作,两个简单接口就可以实现
      • 如何使用SpringBoot写一个属于自己的Starter
        • (一)概述
        • (二)看个例子
        • (三)实现信息播报Starter
        • (四)调用这个Starter
        • (五)总结
      • SpringBoot请求日志,如何优雅地打印
      • 主线程的用户信息,到子线程怎么丢了
    • MQ

    • Zookeeper

    • netty

  • 分布式与微服务

  • 开发经验大全

  • 版本新特性

  • Java
  • 框架的艺术
  • SpringBoot
CodeEase
2023-09-28
目录

如何使用SpringBoot写一个属于自己的Starter

作者:鱼仔
博客首页: codeease.top (opens new window)
公众号:Java鱼仔

# (一)概述

SpringBoot以其自动装配的能力被广泛应用,我们在写代码时肯定遇到过很多spring-boot-starter命名的依赖,比如spring-boot-starter-web,在pom文件中引入这些starter依赖后,SpringBoot就能通过自动装配的技术扫描到这些类并装载到Bean容器中。

除了SpringBoot官方的这些Starter外,我们自己也可以开发Starter。为了和官方的starter区分,建议自定义的starter命名格式为xxxx-spring-boot-starter,比如mybatis-spring-boot-starter。

本文将介绍如何自己实现一个starter。

# (二)看个例子

在写自己的Starter之前,很有必要看一下别人是怎样去写的。这里拿RedisAutoConfiguration这个类作为例子。

SpringBoot在启动时会通过自动装配去扫描项目MATA-INF/spring.factories文件,这个文件中定义了所有需要去自动装配的类。Redis的自动装配类就是下图的RedisAutoConfiguration。

9-1.png

进入 RedisAutoConfiguration后,首先看最重要的几个注解,@Configuration不用多提了,@ConditionalOnClass表示当这个注解后面的类存在时,该Bean才会被加载。下图中很明显RedisOperations并不存在,所以Redis不会被自动装配进去。

9-2.png

@EnableConfigurationProperties用于自动加载配置类的信息,配置类和我们平常写的基本一样,通过ConfigurationProperties读取properties或者yaml中的配置信息。

9-3.png

RedisAutoConfiguration这个类中定义的Bean有两个,我们应该都比较熟悉。@ConditionalOnMissingBean的意思是当Spring容器中不存在这个Bean的时候,才会加载这个Bean,所以如果我们在代码中自己定义了redisTemplate之后,注入到Bean容器中的就是我们自己写的那个Bean。

9-4.png

看到这里基本已经知道一个Starter的实现方案了,接下来就写一个简单的Starter。

# (三)实现信息播报Starter

要做的这个Starter其实很简单,就是输出配置文件中配置的信息。

首先新建一个Maven项目,在项目中新建两个Module,我给两个项目分别命名为report-spring-boot-starter和example-spring-boot

首先介绍report-spring-boot-starter,这是一个Starter,主要实现输出一些内容的功能,我们可以完全按照RedisAutoConfiguration去写。首先新建自动装配类:ReportAutoConfiguration

@Configuration
@ConditionalOnClass(ReportOperation.class)
@EnableConfigurationProperties(ReportProperties.class)
public class ReportAutoConfiguration {
    @Bean
    public ReportOperation reportOperation(ReportProperties reportProperties){
        ReportOperation reportOperation = new ReportOperation(reportProperties.getMsg());
        return  reportOperation;
    }
}
1
2
3
4
5
6
7
8
9
10

当ReportOperation存在时才会加载该配置,激活配置文件ReportProperties

public class ReportOperation {
    private String msg;
    public ReportOperation(String msg){
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
    public String report(){
        return msg;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

ReportProperties如下:

@ConfigurationProperties(prefix = "report")
public class ReportProperties {
    private String msg;

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

接下来是实现自动注入的关键,前面已经说到了,SpringBoot会去扫描依赖Jar包中META-INF/spring.factories中的内容,因此在resources目录下新建META-INF/spring.factories,写下配置信息:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.javayz.starter.ReportAutoConfiguration
1
2

这样一个简单的Starter就完成了。

# (四)调用这个Starter

接下来在example-spring-boot这个module中调用上面的starter,首先第一步引入相关依赖,这里只需要引入springboot相关依赖和自己写的starter依赖即可:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.example</groupId>
    <artifactId>report-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

接着写一个测试的controller测试自动注入的效果:

@RestController
public class TestController {
    @Autowired
    private ReportOperation reportOperation;

    @GetMapping("test")
    public String test(){
        System.out.println(reportOperation.getMsg());
        return reportOperation.getMsg();
    }
}
1
2
3
4
5
6
7
8
9
10
11

在配置文件中增加一条配置:

report.msg = hello
1

运行项目调用接口,可以发现ReportOperation这个Bean已经被自动注入了。

# (五)总结

本文结合redis自动注入的例子,写了一个属于自己的Starter,希望对大家有所帮助。我是鱼仔,我们下期再见!

上次更新: 2025/04/29, 17:22:06
如何在SpringBoot启动时执行初始化操作,两个简单接口就可以实现
SpringBoot请求日志,如何优雅地打印

← 如何在SpringBoot启动时执行初始化操作,两个简单接口就可以实现 SpringBoot请求日志,如何优雅地打印→

最近更新
01
AI大模型部署指南
02-18
02
半个月了,DeepSeek为什么还是服务不可用
02-13
03
Python3.9及3.10安装文档
01-23
更多文章>
Theme by Vdoing | Copyright © 2023-2025 备案图标 浙公网安备33021202002405 | 浙ICP备2023040452号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式