扩展现有的多模块 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 的内容:
-
工具类 / 公共方法
-
字符串处理、日期计算、校验工具
-
例如
StringUtils
,JsonUtils
,Validator
-
-
核心业务逻辑
-
RequestLogger 的日志处理算法
-
数据处理、计算、聚合逻辑
-
-
枚举、常量、DTO
-
公共枚举类、常量类
-
数据传输对象(DTO)
-
-
非 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 就非常灵活,可扩展性也强。