当前位置: 首页 > news >正文

SpringBoot 自定义 starter

 1. 官方文档

SpringBoot 版本 2.6.13,相关链接 Developing with Spring Boot

1.1 什么是 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

The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.

1.2 Starter 命名规范

  • 官方 starter:spring-boot-starter-*
  • 第三方 starter:*-spring-boot-starter

2. 自定义 starter

starter 最令人津津乐道的就是其自动配置特性,我们自定义一个 starter,演示一下该功能。我的上一篇博文,分析了 SpringBoot 的自动配置原理,有兴趣的小伙伴可以移步阅读。

2.1 相关代码准备

2.1.1 创建实体类 UserProperties
@Data
@ConfigurationProperties(prefix = "com.ys.prop")
public class UserProperties {private String version = "1.0";private User user;@Datapublic static class User {private String name;private Integer age;}
}
2.1.2 创建实体类 UserConfiguration
@Data
public class UserConfiguration {private UserProperties userProperties;public UserConfiguration(UserProperties userProperties) {this.userProperties = userProperties;}
}
2.1.3 创建配置类 TestAutoConfiguration
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(UserProperties.class)
public class TestAutoConfiguration {@Beanpublic UserConfiguration userConfiguration(UserProperties properties) {return new UserConfiguration(properties);}
}
2.1.4 创建文件 META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ys.starter.config.TestAutoConfiguration
2.1.5 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>com.ys</groupId><artifactId>test-spring-boot-starter</artifactId><version>1.0</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.6.13</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>2.6.13</version><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><skip>true</skip></configuration></plugin></plugins></build></project>
2.1.6 工程文件结构

2.2 打包部署

2.2.1 执行 mvn clean install,将项目打成 jar 包,部署到本地 maven 仓库

2.2.2 新建一个 Maven 工程,pom 文件添加相关依赖
<dependency><groupId>com.ys</groupId><artifactId>test-spring-boot-starter</artifactId><version>1.0</version>
</dependency>

2.3 现象演示

2.3.1 启动项目,运行 main 方法
@SpringBootApplication
public class BlogApplication {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(BlogApplication.class, args);Object bean = context.getBean(TestAutoConfiguration.class);System.out.println(bean);}
}

通过日志,得出结论:自定义 starter 的自动配置生效

2.3.2 覆盖默认属性

在 test-spring-boot-starter 中 @EnableConfigurationProperties + @ConfigurationProperties 注解的组合使用,并且添加依赖 spring-boot-configuration-processor,我们可以在 idea 中根据提示覆盖默认属性,这也是 SpringBoot 一些自动配置类的常用套路,相关应用可以查看 WebMvcAutoConfiguration 的内部类 WebMvcAutoConfigurationAdapter

2.3.2.1 演示覆盖默认属性值
application.yaml 配置
com:ys:prop:version: 2.0user:name: annaage: 18
启动项目,运行 main 方法
@SpringBootApplication
public class BlogApplication {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(BlogApplication.class, args);Object bean = context.getBean(UserConfiguration.class);System.out.println(bean);}
}

通过日志,得出结论:默认属性被覆盖

2.3.3 自动配置类一些特征

自动配置类一般都是以 AutoConfiguration 结尾,我们可以通过搜索  AutoConfiguration 来锁定相关类

2.4 扩展:解析以 .json 为后缀的配置文件

starter 可以扩展很多东西,我们不要陷入误区,认为 starter 等价于自动配置。下文将对自定义的 starter 进行扩展,让它可以解析以 .json 为后缀的配置文件

2.4.1 配置文件前置知识点
2.4.1.1 默认配置文件位置
  • optional:classpath:/
  • optional:classpath:/config/
  • optional:file:./
  • optional:file:./config/
  • optional:file:./config/*/
2.4.1.2 默认配置文件前缀
  • application
2.4.1.3 默认配置文件后缀
  • yml
  • yaml
  • properties
  • xml
2.4.1.4 默认配置文件

所以默认情况下,以下文件(文件在指定位置)都可以认为是配置文件:

  • application.yml
  • application.yaml
  • application.properties
  • application.xml

更多配置相关知识,可以移步相关博文:SpringBoot之外部化配置

2.4.2 让 SpringBoot 将 application.json 也当成配置文件解析
2.4.2.1 相关代码准备
2.4.2.1.1 创建工具类 JsonToPropertySourceConverter
public class JsonToPropertySourceConverter {public static PropertySource<?> convertJsonToPropertySource(String name, String json) {try {ObjectMapper objectMapper = new ObjectMapper();Map<String, Object> map = objectMapper.readValue(json, Map.class);return new MapPropertySource(name, map);} catch (Exception e) {throw new RuntimeException(e);}}
}
2.4.2.1.2 创建Loader JsonSourceLoader
public class JsonSourceLoader implements PropertySourceLoader {@Overridepublic String[] getFileExtensions() {return new String[]{"json"};}@Overridepublic List<PropertySource<?>> load(String name, Resource resource) throws IOException {List<PropertySource<?>> result = new ArrayList<>();if (resource == null || !resource.exists()) {return result;}ByteArrayOutputStream bao = new ByteArrayOutputStream();try (InputStream in = resource.getInputStream()) {IOUtils.copy(in, bao);}String json = new String(bao.toByteArray(), StandardCharsets.UTF_8);PropertySource<?> propertySource = JsonToPropertySourceConverter.convertJsonToPropertySource(resource.getFilename(), json);result.add(propertySource);return result;}
}
2.4.2.1.3 在 spring.factories 中添加 key 为 PropertySourceLoader 的配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ys.starter.config.TestAutoConfigurationorg.springframework.boot.env.PropertySourceLoader=\
com.ys.starter.loader.JsonSourceLoader
2.4.2.1.4 重新打包部署 test-spring-boot-starter

执行命令 mvn clean install

2.4.2.2 现象演示
2.4.2.2.1 创建文件 application.json

在依赖 test-spring-boot-starter.jar 的项目里创建文件 application.json,文件明细如下:

{"my.starter.key": "666"
}
2.4.2.2.2 启动项目,运行 main 方法
@SpringBootApplication
public class BlogApplication {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(BlogApplication.class, args);String property = context.getEnvironment().getProperty("my.starter.key");System.out.println(property);}
}

通过日志,得出结论:成功获取到属性 my.starter.key 的值,即 application.json 被当成配置文件解析

相关文章:

SpringBoot 自定义 starter

1. 官方文档 SpringBoot 版本 2.6.13&#xff0c;相关链接 Developing with Spring Boot 1.1 什么是 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 relate…...

TDengine Invalid data format 问题定位

Invalid data format 看语义是数据类型不符&#xff0c;通常这个报错出现在使用行协议写入时。 如果是批量数据写入&#xff0c;想定位是哪条语句的问题&#xff0c;需要查看客户端日志。 如何确定使用的是哪个日志 lsof -p pidof taosadapter | grep taoslog如果没有安装lso…...

Spring Boot 使用 MongoDB 教程

&#x1f341; 作者&#xff1a;知识浅谈&#xff0c;CSDN签约讲师&#xff0c;CSDN博客专家&#xff0c;华为云云享专家&#xff0c;阿里云专家博主 &#x1f4cc; 擅长领域&#xff1a;全栈工程师、爬虫、ACM算法 &#x1f525; 微信&#xff1a;zsqtcyw 联系我领取学习资料 …...

Python办公自动化:使用openpyxl 创建与保存 Excel 工作簿

1 创建新的工作簿 在开始任何 Excel 操作之前&#xff0c;首先需要创建一个工作簿。openpyxl 提供了简单的接口来创建新的工作簿。 创建一个空白的工作簿 我们可以使用 openpyxl.Workbook() 来创建一个新的空白工作簿。以下是一个简单的示例&#xff1a; import openpyxl# …...

【张】#11 Union 共用体

Union 共用体可以存储不同的数据类型&#xff0c;但只能同时存储其中的一种类型。 #include <iostream> using namespace std;struct Product {char productName[20];int type;//1 int ,else charunion{int id_int;char id_chars[20];}; };int main(){Product product; …...

Xcode 在原生集成flutter项目

笔者公司有一个从2017年就开始开发的iOS和安卓原生项目&#xff0c;现在计划从外到内开始进行项目迁徙。 1》从gitee拉取flutter端的代码&#xff1b;&#xff08;Android报错Exception: Podfile missing&#xff09; 2》替换Xcode里的cocopods里Podfile的路径 然后报警 然后…...

ES6的promise

Promise是什么 1、Promise是js中的一个原生对象&#xff0c;是一种异步编程的解决方案。可以替换掉传统的回调函数解决方案&#xff0c;将异步操作以同步的流程表达出来。 2、Promise有三种状态&#xff1a;pending(初始化)、fulfilled(成功)、rejected(失败) 可以通过resolve(…...

轻松找回:如何在PostgreSQL 16中重置忘记的数据库密码

目录 1. 引言2. PostgreSQL 16的新特性简介3. 解决方法概述4. 方法一&#xff1a;通过修改pg_hba.conf文件重置密码5. 方法二&#xff1a;通过命令行进入单用户模式6. 方法三&#xff1a;使用pgAdmin工具重置密码7. 总结与最佳实践写在以后 1. 引言 你有没有过这样的经历&…...

EVAL长度突破限制

目录 突破15位限制 代码 绕过方式 第一种&#xff08;使用echo执行&#xff09; 第二种&#xff08;使用file_get_content追加文件后进行问件包含&#xff09; 第三种&#xff08;使用usort可变长参数&#xff09; 突破7位限制 第一种&#xff08;可以使用>创建文件…...

如何判断树上一个点是否在直径上

# 旅游规划 ## 题目描述 W市的交通规划出现了重大问题&#xff0c;市政府下定决心在全市各大交通路口安排疏导员来疏导密集的车流。但由于人员不足&#xff0c;W市市长决定只在最需要安排人员的路口安排人员。 具体来说&#xff0c;W市的交通网络十分简单&#xff0c;由n个…...

docker 部署 RabbitMQ

命令 docker run -d --namerabbitmq \ -p 5671:5671 -p 5672:5672 -p 4369:4369 \ -p 15671:15671 -p 15672:15672 -p 25672:25672 \ -e RABBITMQ_DEFAULT_USERusername\ -e RABBITMQ_DEFAULT_PASSpassword\ -v /usr/local/rabbitmq/data:/var/lib/rabbitmq \ -v /usr/local/r…...

设计模式 - 过滤器模式

💝💝💝首先,欢迎各位来到我的博客!本文深入理解设计模式原理、应用技巧、强调实战操作,提供代码示例和解决方案,适合有一定编程基础并希望提升设计能力的开发者,帮助读者快速掌握并灵活运用设计模式。 💝💝💝如有需要请大家订阅我的专栏【设计模式】哟!我会定…...

使用 Locust 进行本地压力测试

在应用开发和运维过程中&#xff0c;了解应用在高负载情况下的表现至关重要。压力测试可以帮助你识别性能瓶颈和潜在问题。本文将介绍如何使用 Locust 工具进行本地压力测试&#xff0c;模拟高并发场景&#xff0c;并分析测试结果。 1. 什么是 Locust&#xff1f; Locust 是一…...

【图形学】TA之路-矩阵应用平移-旋转-大小

矩阵应用&#xff1a;在 Unity 中&#xff0c;Transform 和矩阵之间的关系非常密切。Transform 组件主要用于描述和控制一个物体在三维空间中的位置、旋转和缩放&#xff0c;而这些操作背后实际上都是通过矩阵来实现的 1. Transform 组件与矩阵的关系 Transform 组件包含以下…...

Spring 循环依赖解决方案

文章目录 1. 循环依赖的产生2. 循环依赖的解决模型3. 基于setter/Autowired 的循环依赖1_编写测试代码2_初始化 Cat3_初始化 Person4_ 回到 Cat 的创建流程5_小结 4. 基于构造方法的循环依赖5. 基于原型 Bean 的循环依赖6. 引人AOP的额外设计7. 总结 IOC 容器初始化bean对象的逻…...

可视化大屏:如何get到领导心目中的“科技感”?

你如果问领导可视化大屏需要什么风格的&#xff0c;领导大概率说科技感的&#xff0c;然后你就去做了&#xff0c;结果被劈了一顿&#xff0c;什么原因&#xff1f;因为你没有get到领导心目中描述的科技感。 一、为什么都喜欢科技感 科技感在可视化大屏设计中具有以下好处&am…...

基于Python的金融数据采集与分析的设计与实现

基于Python的金融数据采集与分析的设计与实现 “Design and Implementation of Financial Data Collection and Analysis based on Python” 完整下载链接:基于Python的金融数据采集与分析的设计与实现 文章目录 基于Python的金融数据采集与分析的设计与实现摘要第一章 绪论1…...

使用Sanic和SSE实现实时股票行情推送

&#x1f49d;&#x1f49d;&#x1f49d;欢迎莅临我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐&#xff1a;「storm…...

redis散列若干记录

字典 redis本身使用字典结构管理数据 redis使用hash表实现字典结构 使用了什么hash算法 使用SipHash算法&#xff0c;该算法能有效防止Hash表碰撞&#xff0c;并有不错的性能 hash冲突怎么解决 使用链表法解决hash冲突 hash表如何扩容 渐进式扩容&#xff0c;不会引起线程长期阻…...

Java面试八股之什么是STOMP协议

什么是STOMP协议 STOMP&#xff08;Simple Text Oriented Messaging Protocol&#xff09;是一种为消息队列和事件驱动架构设计的轻量级协议&#xff0c;主要用于在消息中间件之间进行消息交换。它的设计原则是简单、跨平台和易于实现&#xff0c;这使得STOMP成为许多实时应用…...

Unity3D中Gfx.WaitForPresent优化方案

前言 在Unity中&#xff0c;Gfx.WaitForPresent占用CPU过高通常表示主线程在等待GPU完成渲染&#xff08;即CPU被阻塞&#xff09;&#xff0c;这表明存在GPU瓶颈或垂直同步/帧率设置问题。以下是系统的优化方案&#xff1a; 对惹&#xff0c;这里有一个游戏开发交流小组&…...

从零实现富文本编辑器#5-编辑器选区模型的状态结构表达

先前我们总结了浏览器选区模型的交互策略&#xff0c;并且实现了基本的选区操作&#xff0c;还调研了自绘选区的实现。那么相对的&#xff0c;我们还需要设计编辑器的选区表达&#xff0c;也可以称为模型选区。编辑器中应用变更时的操作范围&#xff0c;就是以模型选区为基准来…...

React Native在HarmonyOS 5.0阅读类应用开发中的实践

一、技术选型背景 随着HarmonyOS 5.0对Web兼容层的增强&#xff0c;React Native作为跨平台框架可通过重新编译ArkTS组件实现85%以上的代码复用率。阅读类应用具有UI复杂度低、数据流清晰的特点。 二、核心实现方案 1. 环境配置 &#xff08;1&#xff09;使用React Native…...

MODBUS TCP转CANopen 技术赋能高效协同作业

在现代工业自动化领域&#xff0c;MODBUS TCP和CANopen两种通讯协议因其稳定性和高效性被广泛应用于各种设备和系统中。而随着科技的不断进步&#xff0c;这两种通讯协议也正在被逐步融合&#xff0c;形成了一种新型的通讯方式——开疆智能MODBUS TCP转CANopen网关KJ-TCPC-CANP…...

TRS收益互换:跨境资本流动的金融创新工具与系统化解决方案

一、TRS收益互换的本质与业务逻辑 &#xff08;一&#xff09;概念解析 TRS&#xff08;Total Return Swap&#xff09;收益互换是一种金融衍生工具&#xff0c;指交易双方约定在未来一定期限内&#xff0c;基于特定资产或指数的表现进行现金流交换的协议。其核心特征包括&am…...

根据万维钢·精英日课6的内容,使用AI(2025)可以参考以下方法:

根据万维钢精英日课6的内容&#xff0c;使用AI&#xff08;2025&#xff09;可以参考以下方法&#xff1a; 四个洞见 模型已经比人聪明&#xff1a;以ChatGPT o3为代表的AI非常强大&#xff0c;能运用高级理论解释道理、引用最新学术论文&#xff0c;生成对顶尖科学家都有用的…...

DeepSeek 技术赋能无人农场协同作业:用 AI 重构农田管理 “神经网”

目录 一、引言二、DeepSeek 技术大揭秘2.1 核心架构解析2.2 关键技术剖析 三、智能农业无人农场协同作业现状3.1 发展现状概述3.2 协同作业模式介绍 四、DeepSeek 的 “农场奇妙游”4.1 数据处理与分析4.2 作物生长监测与预测4.3 病虫害防治4.4 农机协同作业调度 五、实际案例大…...

Typeerror: cannot read properties of undefined (reading ‘XXX‘)

最近需要在离线机器上运行软件&#xff0c;所以得把软件用docker打包起来&#xff0c;大部分功能都没问题&#xff0c;出了一个奇怪的事情。同样的代码&#xff0c;在本机上用vscode可以运行起来&#xff0c;但是打包之后在docker里出现了问题。使用的是dialog组件&#xff0c;…...

Java求职者面试指南:Spring、Spring Boot、MyBatis框架与计算机基础问题解析

Java求职者面试指南&#xff1a;Spring、Spring Boot、MyBatis框架与计算机基础问题解析 一、第一轮提问&#xff08;基础概念问题&#xff09; 1. 请解释Spring框架的核心容器是什么&#xff1f;它在Spring中起到什么作用&#xff1f; Spring框架的核心容器是IoC容器&#…...

搭建DNS域名解析服务器(正向解析资源文件)

正向解析资源文件 1&#xff09;准备工作 服务端及客户端都关闭安全软件 [rootlocalhost ~]# systemctl stop firewalld [rootlocalhost ~]# setenforce 0 2&#xff09;服务端安装软件&#xff1a;bind 1.配置yum源 [rootlocalhost ~]# cat /etc/yum.repos.d/base.repo [Base…...