手写 Starter 进阶:@ConfigurationProperties 实战(支持 application.yml)

张开发
2026/4/19 20:00:37 15 分钟阅读

分享文章

手写 Starter 进阶:@ConfigurationProperties 实战(支持 application.yml)
一、前言上一节我们已经做了一个最小 starter手写一个最小 Starter从 0 到能看懂ark-hello-starter但它有个问题❗功能是写死的用户不能配置比如return hello from ark starter; 企业里肯定不行。二、本篇目标让用户可以这样用ark:hello:enabled: truemessage: hello world然后helloService.sayHello()返回hello world三、核心新增一个东西ConfigurationProperties它的作用一句话把配置文件yml自动映射成 Java 对象四、第一步定义配置类package com.ark.starter.properties; import org.springframework.boot.context.properties.ConfigurationProperties; ConfigurationProperties(prefix ark.hello) public class HelloProperties { /** * 是否开启 */ private boolean enabled true; /** * 提示语 */ private String message hello from ark starter; // getter / setter public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled enabled; } public String getMessage() { return message; } public void setMessage(String message) { this.message message; } }五、第二步让配置类生效在自动配置类中加import org.springframework.boot.context.properties.EnableConfigurationProperties; Configuration EnableConfigurationProperties(HelloProperties.class) public class HelloAutoConfiguration { } 这句话的作用把HelloProperties注册到 Spring 容器里六、第三步改造 HelloService让它支持配置package com.ark.starter.service; import com.ark.starter.properties.HelloProperties; public class HelloService { private final HelloProperties properties; public HelloService(HelloProperties properties) { this.properties properties; } public String sayHello() { return properties.getMessage(); } }七、第四步改造自动配置类package com.ark.starter.autoconfigure; import com.ark.starter.properties.HelloProperties; import com.ark.starter.service.HelloService; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration ConditionalOnClass(HelloService.class) public class HelloAutoConfiguration { Bean ConditionalOnMissingBean public HelloService helloService(HelloProperties properties) { return new HelloService(properties); } }八、再升级一步支持开关控制 用你刚学的条件注解import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;改成这样Configuration ConditionalOnClass(HelloService.class) public class HelloAutoConfiguration { Bean ConditionalOnMissingBean ConditionalOnProperty(prefix ark.hello, name enabled, havingValue true, matchIfMissing true) public HelloService helloService(HelloProperties properties) { return new HelloService(properties); } }这句非常关键enabled true → 生效enabled false → 不加载没写 → 默认生效九、用户项目怎么用1️⃣ 引入 starterdependency groupIdcom.ark/groupId artifactIdark-hello-starter/artifactId version1.0.0/version /dependency2️⃣ 写配置ark:hello:enabled: truemessage: 你好我是自定义配置3️⃣ 直接使用RestController public class TestController { private final HelloService helloService; public TestController(HelloService helloService) { this.helloService helloService; } GetMapping(/hello) public String hello() { return helloService.sayHello(); } }访问GET /hello返回你好我是自定义配置十、你现在彻底理解一件事starter 的完整能力已经出来了Starter 三件套你必须记住1. 自动配置类Configuration2. 条件控制Conditional3. 配置绑定ConfigurationProperties十一、和你 Android 经验对齐这块你会更稳Androidgradle.properties / build.gradle ↓ 配置参数 ↓ SDK读取配置 ↓ 改变行为Spring Bootapplication.yml ↓ ConfigurationProperties ↓ 自动注入 ↓ 改变 Bean 行为 本质一样配置驱动行为十二、企业级 starter 一般还会做什么你知道就够默认配置多实现切换比如 Redis / KafkaSPI扩展日志增强metrics监控AOP增强十三、能力评估如果以下都理解或做到理解 starter 原理理解自动装配能写最小 starter能做配置驱动 这已经是中级后端 架构意识初级十四、总结一句话❗ starter 的终极目标就是让别人“加一个依赖 写一点配置”就能用你的功能最后给你一句很关键的话你现在不是“不会 starter”你是已经跨过了最难的理解门槛只差熟练度下一步再往上做一个更真实的 “ark-redis-starter带缓存 开关 降级策略”这个就已经接近面试能拿来讲的项目级能力了。

更多文章