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

自定义SpringBoot Starter-笔记

SpringBoot Starter的介绍参考: Spring Boot Starter简介-笔记-CSDN博客。这里介绍如何自定义一个springBoot Starter。

1. 项目结构

创建一个 Maven 项目,结构如下:

custom-spring-boot-starter-demo/
├── custom-hello-jdk/  # jdk模块,包含功能逻辑
├── custom-hello-spring-boot-starter/  #Starter模块

2. 项目代码

2.1 custom-hello-jdk模块

step1. pom.xml

<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>custom-hello-jdk</artifactId><version>1.0.0</version><dependencies><!-- Spring Boot 自动配置依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency></dependencies>
</project>

step2. 定义功能接口和实现类

package com.example.demo;public interface HelloService {String sayHello();
}public class DefaultHelloService implements HelloService {private final String message;public DefaultHelloService(String message) {this.message = message;}@Overridepublic String sayHello() {return message;}
}

step3. 配置属性类

使用配置属性类实现属性的灵活绑定:

package com.example.demo;import org.springframework.boot.context.properties.ConfigurationProperties;@Data
@ConfigurationProperties(prefix = "custom.hello")
public class HelloProperties {private String message = "Hello from Custom Starter!";
}

step4. 自动配置类

package com.example.demo;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {private final HelloProperties helloProperties;public HelloAutoConfiguration(HelloProperties helloProperties) {this.helloProperties = helloProperties;}@Bean@ConditionalOnMissingBean(HelloService.class)public HelloService helloService() {return new DefaultHelloService(helloProperties.getMessage());}
}

2.2 custom-hello-spring-boot-starter模块

step1.pom.xml添加对custom-hello-jdk的依赖

<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>custom-hello-spring-boot-starter</artifactId><version>1.0.0</version><packaging>pom</packaging><dependencies><!-- 引入jdk模块 --><dependency><groupId>com.example</groupId><artifactId>custom-hello-jdk</artifactId><version>1.0.0</version></dependency></dependencies>
</project>

step2.注册自动配置

在 src/main/resources/META-INF/spring.factories 中添加:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.demo.HelloAutoConfiguration

备注:spring.factories文件也可以放到custom-hello-jdk模块里,在starter里仅放pom文件,作为依赖聚合,方便用户引入。

step3.构建和发布

  1. 执行 mvn clean install 将 Starter 安装到本地 Maven 仓库
  2. 或通过 mvn deploy 发布到远程仓库

2.3 使用自定义 Starter

背景:在另外一个项目中使用自定义的Starter。

step1. 在另一个 Spring Boot 项目中引入依赖

<dependency><groupId>com.example</groupId><artifactId>custom-hello-spring-boot-starter</artifactId><version>1.0.0</version>
</dependency>

step2. 配置属性(可选)

若不配置,则使用HelloProperties中设置的默认值:

custom.hello.message=Hello from Config!

step3.使用Bean

可直接使用@Autowired引入目标bean:

import com.example.demo.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@Autowiredprivate HelloService helloService;@GetMapping("/hello")public String hello() {return helloService.sayHello();}
}

2.4 总结

上述自定义springBoot Starter的完整流程说明:

  1. jdk功能模块:包含实际功能代码、配置属性等
  2. Starter 模块:作为依赖聚合,方便用户引入
  3. 条件化配置:通过 @ConditionalOnMissingBean 避免重复 Bean
  4. 属性绑定:使用 @ConfigurationProperties 实现灵活配置
http://www.xdnf.cn/news/4323.html

相关文章:

  • Element-Plus-X开源程序是Vue3 + Element-Plus 开箱即用的企业级AI组件库前端的解决方案
  • 【言语理解】片段阅读之语句填入(7)
  • LeetCode 1781. 所有子字符串美丽值之和 题解
  • C++编程语言:从高效系统开发到现代编程范式的演进之路
  • python仓库库存管理系统-药房药品库存管理系统
  • 极简RT-Thread入门教程
  • 高等数学第六章---定积分(§6.1元素法6.2定积分在几何上的应用1)
  • XILINX原语之——xpm_fifo_async(异步FIFO灵活设置位宽、深度)
  • vscode远程服务器连接----过程尝试写入的管道不存在
  • javascript Map 和对象使用
  • echarts报错问题initialize failed:invalid dom
  • AI技术下研发体系重构
  • Vue项目Git提交流程集成
  • Leetcode 刷题记录 07 —— 链表
  • excel表数据导入数据库
  • Selenium模拟人类,操作网页的行为(全)
  • Pointpillars(三)工程实践
  • 新手SEO基础操作入门精要
  • Java学习手册:Base64 编码概念和应用场景
  • 解锁创意显示,强力巨彩软模组引领柔性显示技术创新
  • 随机快速排序算法
  • GAN模型
  • 总结七种提示优化方案的核心实现流程
  • 第15章 Python数据类型详解之分解理解:基础数据类型常见易错点和性能优化篇
  • Visual Studio 快捷键更改和设置
  • 【C++游戏引擎开发】第30篇:物理引擎(Bullet)—软体动力学系统
  • Java开发 自定义注解(更新中)
  • MySQL 常用函数分类
  • 编程日志4.25
  • 十分钟了解 @MapperScan