一、前言
上一节我们已经做了一个最小 starter:
手写一个最小 Starter:从 0 到能看懂
ark-hello-starter
但它有个问题:
❗功能是写死的,用户不能配置
比如:return "hello from ark starter";
👉 企业里肯定不行。
二、本篇目标
让用户可以这样用:
ark:
hello:
enabled: true
message: 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️⃣ 引入 starter
<dependency> <groupId>com.ark</groupId> <artifactId>ark-hello-starter</artifactId> <version>1.0.0</version> </dependency>2️⃣ 写配置
ark:
hello:
enabled: true
message: 你好,我是自定义配置
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. 自动配置类(@Configuration)
2. 条件控制(@Conditional)
3. 配置绑定(@ConfigurationProperties)
十一、和你 Android 经验对齐(这块你会更稳)
Android
gradle.properties / build.gradle ↓ 配置参数 ↓ SDK读取配置 ↓ 改变行为Spring Boot
application.yml ↓ @ConfigurationProperties ↓ 自动注入 ↓ 改变 Bean 行为👉 本质一样:
配置驱动行为
十二、企业级 starter 一般还会做什么(你知道就够)
- 默认配置
- 多实现切换(比如 Redis / Kafka)
- SPI扩展
- 日志增强
- metrics监控
- AOP增强
十三、能力评估
如果以下都理解或做到:
- 理解 starter 原理
- 理解自动装配
- 能写最小 starter
- 能做配置驱动
👉 这已经是:中级后端 + 架构意识初级
十四、总结一句话
❗ starter 的终极目标就是:
让别人“加一个依赖 + 写一点配置”就能用你的功能
最后给你一句很关键的话
你现在不是“不会 starter”,
你是:
已经跨过了最难的理解门槛,只差熟练度
下一步再往上,做一个更真实的:
👉 “ark-redis-starter(带缓存 + 开关 + 降级策略)”
这个就已经接近面试能拿来讲的项目级能力了。