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

扩展现有的多模块 Starter

目录

一、引言

✅ 适合放在 core 的内容:

⚠️ 注意事项

二、核心模块 my-feature-core 扩展

1、RateLimiter.java

2、RequestLogger.java

三、自动配置模块 my-feature-spring-boot-autoconfigure 扩展

1、配置类 RateLimiterProperties.java

2、自动配置类 RequestLoggerAutoConfiguration.java 扩展

3、自动配置声明 AutoConfiguration.imports

4、IDE 配置提示 additional-spring-configuration-metadata.json 扩展

四、Demo 测试项目扩展

1、application.yml

2、TestController.java 扩展

五、使用效果

六、总结


一、引言

my-feature-core 模块的设计初衷是 放置与 Spring Boot 无关的核心逻辑,因此你可以在里面写各种业务逻辑、工具类、算法、公共方法等。关键原则是 不依赖 Spring Boot 注解或容器,保持纯粹的 Java 可复用性。

✅ 适合放在 core 的内容:

  1. 工具类 / 公共方法

    • 字符串处理、日期计算、校验工具

    • 例如 StringUtils, JsonUtils, Validator

  2. 核心业务逻辑

    • RequestLogger 的日志处理算法

    • 数据处理、计算、聚合逻辑

  3. 枚举、常量、DTO

    • 公共枚举类、常量类

    • 数据传输对象(DTO)

  4. 非 Spring 环境也能使用的功能

    • 例如在普通 Java 项目、测试工具里调用 core 方法


⚠️ 注意事项

  • 不要在 core 引入 Spring Boot 依赖

    • 保持可复用性

    • 避免与自动配置或 starter 模块耦合

  • 不要直接使用 @Configuration、@Bean 等注解

    • 这些属于 autoconfigure 模块的职责

接下来,在上文基础上多模块 Starter 最佳实践(强烈推荐!!!)我将扩展现有的多模块 Starter 示例,增加一个 RateLimiter 功能,并展示如何在 autoconfigure 模块注册多个 Bean,让业务项目开箱即用。


二、核心模块 my-feature-core 扩展

1、RateLimiter.java

 
package com.example.myfeature.core;import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;/*** 核心逻辑模块:限流工具* 纯 Java 类,不依赖 Spring Boot*/
public class RateLimiter {private final ConcurrentHashMap<String, AtomicInteger> counterMap = new ConcurrentHashMap<>();private final int maxRequests;/*** 构造函数* @param maxRequests 每个 key 最大请求次数*/public RateLimiter(int maxRequests) {this.maxRequests = maxRequests;}/*** 尝试获取访问权限* @param key 限流标识* @return true 表示允许,false 表示超过限制*/public boolean tryAcquire(String key) {AtomicInteger counter = counterMap.computeIfAbsent(key, k -> new AtomicInteger(0));return counter.incrementAndGet() <= maxRequests;}
}

2、RequestLogger.java

保持不变,仍然放核心日志逻辑。


三、自动配置模块 my-feature-spring-boot-autoconfigure 扩展

1、配置类 RateLimiterProperties.java

 
package com.example.myfeature.autoconfigure;import org.springframework.boot.context.properties.ConfigurationProperties;/*** RateLimiter 配置属性类*/
@ConfigurationProperties(prefix = "myfeature.ratelimiter")
public class RateLimiterProperties {/*** 每个 key 最大请求次数*/private int maxRequests = 10;public int getMaxRequests() {return maxRequests;}public void setMaxRequests(int maxRequests) {this.maxRequests = maxRequests;}
}

2、自动配置类 RequestLoggerAutoConfiguration.java 扩展

 
package com.example.myfeature.autoconfigure;import com.example.myfeature.core.RateLimiter;
import com.example.myfeature.core.RequestLogger;
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({RequestLoggerProperties.class, RateLimiterProperties.class})
public class RequestLoggerAutoConfiguration {@Bean@ConditionalOnMissingBeanpublic RequestLogger requestLogger(RequestLoggerProperties props) {return new RequestLogger(props.isEnableBody(), props.getMaxBodyLength());}@Bean@ConditionalOnMissingBeanpublic RateLimiter rateLimiter(RateLimiterProperties props) {return new RateLimiter(props.getMaxRequests());}
}


3、自动配置声明 AutoConfiguration.imports

内容保持不变:

com.example.myfeature.autoconfigure.RequestLoggerAutoConfiguration 

4、IDE 配置提示 additional-spring-configuration-metadata.json 扩展

{"properties": [{"name": "myfeature.logger.enable-body","type": "java.lang.Boolean","description": "Enable request body logging","defaultValue": true},{"name": "myfeature.logger.max-body-length","type": "java.lang.Integer","description": "Maximum length of request body to log","defaultValue": 1000},{"name": "myfeature.ratelimiter.max-requests","type": "java.lang.Integer","description": "Maximum requests per key for RateLimiter","defaultValue": 10}]
}

四、Demo 测试项目扩展

1、application.yml

 
myfeature:logger:enable-body: truemax-body-length: 500ratelimiter:max-requests: 5

2、TestController.java 扩展

 
package com.example.demo;import com.example.myfeature.core.RateLimiter;
import com.example.myfeature.core.RequestLogger;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/test")
public class TestController {private final RequestLogger logger;private final RateLimiter rateLimiter;public TestController(RequestLogger logger, RateLimiter rateLimiter) {this.logger = logger;this.rateLimiter = rateLimiter;}@PostMappingpublic String log(@RequestBody String body, @RequestParam String key) {if (!rateLimiter.tryAcquire(key)) {return "Rate limit exceeded for key: " + key;}logger.log(body);return "ok";}
}

五、使用效果

1、启动 demo 项目:

mvn spring-boot:run -pl demo-spring-boot-app

2、测试接口

# 允许前 5 次请求
curl -X POST "http://localhost:8080/test?key=user1" -d "Hello"
# 超过限制
curl -X POST "http://localhost:8080/test?key=user1" -d "Hello"

3、控制台输出请求日志,同时超过限制时返回:

Rate limit exceeded for key: user1

Rate limit exceeded for key: user1


✅ 这样,你的 Starter 就支持 多个功能 Bean

  • RequestLogger → 请求日志

  • RateLimiter → 限流

优势

  • core 模块可扩展其他纯 Java 功能

  • autoconfigure 模块统一注册 Bean

  • starter 聚合所有功能,用户只需引入依赖即可使用

六、总结

简单总结:

  • core = 纯逻辑 + 工具 + DTO

  • autoconfigure = Spring Boot 适配 + Bean 注册

  • starter = 聚合依赖

这样你的 Starter 就非常灵活,可扩展性也强。

http://www.dtcms.com/a/352152.html

相关文章:

  • 2025本地部署overleaf
  • 售价3499美元,英伟达Jetson Thor实现机器人与物理世界的实时智能交互
  • 09-SpringBoot入门案例
  • 嵌入式学习笔记-LINUX系统编程阶段-DAY01脚本
  • 第四章:条件判断
  • VueFlow画布可视化——js技能提升
  • 安全测试、web探测、httpx
  • vue2和vue3的对比
  • Android 属性系统
  • 蓝思科技中报:深耕业务增量,AI硬件打开想象空间
  • Pandas vs Polars Excel 数据加载对比报告
  • Coze Studio系统架构深度剖析:从分层设计到领域驱动的技术实践- 第二篇
  • vue实现拖拉拽效果,类似于禅道首页可拖拽排布展示内容(插件-Grid Layout)
  • 用 Allure 生成 pytest 测试报告:从安装到使用全流程
  • STM32 定时器(互补输出+刹车)
  • yggjs_rbutton React按钮组件v1.0.0 多主题系统使用指南
  • 什么叫API对接HR系统?
  • 2025年8月技术问答第3期
  • 03MySQL——DCL权限控制,四种常用函数解析
  • SSM入门到实战: 3.6 SpringMVC RESTful API开发
  • 基于muduo库的图床云共享存储项目(一)
  • vs2019安装cpu版本的fftw 以实现傅里叶变换
  • 《护理学》10月版面征稿论文速递
  • 【46页PPT】AI智能中台用ABC+IOT重新定义制造(附下载方式)
  • SQLBot:一款基于大语言模型和RAG的智能数据分析工具
  • AI人工智能一体化HR系统如何选型?
  • 重塑金融管理会计核心引擎,容智管会智能体打造智能决策新基建
  • 手写MyBatis第35弹:@Select、@Insert等注解的背后原理
  • 【软考论文】论DevOps及其应用
  • BotCash:2025 中国算力大会——国家级数据标注产业供需对接大会