当前位置: 首页 > 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.xdnf.cn/news/1367893.html

相关文章:

  • 【yocto】Yocto Project 核心:深入了解.bbclass文件
  • DevSecOps 集成 CI/CD Pipeline:实用指南
  • OnlyOffice ARM 架构编译教程
  • Hive的核心架构
  • 科普 | 5G支持的WWC架构是个啥(1)?
  • Spring Boot 集成 Docker 构建与发版完整指南
  • Netty源码—性能优化和设计模式
  • (vue)el-progress左侧添加标签/名称
  • 机器视觉学习-day02-灰度化实验
  • 【云计算】云原生(Cloud Native)
  • Spark云原生流处理实战与风控应用
  • 【云原生】CentOS安装Kubernetes+Jenkins
  • 【语法】【C+V】本身常用图表类型用法快查【CSDN不支持,VSCODE可用】
  • 云计算学习笔记——Linux硬盘、硬盘划分、交换空间、自动挂载篇
  • CentOS 7服务器初始化全攻略:从基础配置到安全加固
  • Redis ZSET 深度剖析:从命令、原理到实战
  • 几种方式实现文件自动上传到服务器共享文件夹
  • NVIDIA GPU 中的 L2 Cache
  • 【Linux】Socket编程——TCP版
  • 深入OpenHarmony后台任务“黑匣子”:BackgroundTaskMgr框架全栈解析与实战避坑指南
  • Thingsboard 租户管理员权限,增加租户普通用户权限
  • 三、显示3D文字
  • PLC通讯中遇到的实际场景
  • Mamba-HoME:面向3D医学影像分割的层次化专家混合新框架
  • 自然处理语言NLP: 基于双分支 LSTM 的酒店评论情感分析模型构建与实现
  • 透视光合组织大会:算力生态重构金融AI落地新实践
  • C语言 指针
  • 【设计模式】 面向对象基础
  • 打破技术壁垒的先进制造框架的智慧工业开源了
  • 如何利用ArcGIS探究环境与生态因子对水体、土壤、大气污染物的影响?