news 2026/4/19 20:00:34

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
手写 Starter 进阶:@ConfigurationProperties 实战(支持 application.yml)

一、前言

上一节我们已经做了一个最小 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(带缓存 + 开关 + 降级策略)”

这个就已经接近面试能拿来讲的项目级能力了。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/19 20:00:31

Spring Boot 手写 Starter 实战:带开关 + 自动装配 + 日志模块

一、前言很多人学 Spring Boot 时&#xff0c;会用各种 starter&#xff1a;spring-boot-starter-webspring-boot-starter-data-redisspring-boot-starter-aop但用久了&#xff0c;容易产生一个误区&#xff1a;觉得 starter 就是“一个依赖包”。其实不是。starter 真正的核心…

作者头像 李华
网站建设 2026/4/19 19:59:38

RAG 只是权宜之计

在我第一个生产级 RAG 系统上线三个月后&#xff0c;我在晚上 11 点收到了告警。 一个企业客户的聊天机器人检索到了一条完全针对不同员工层级的 HR 政策。语言足够相似&#xff0c;以至于检索器认为匹配。实际上并没有。我花了两天时间调优&#xff0c;更小的分块、更大的重叠…

作者头像 李华
网站建设 2026/4/19 19:57:02

BMP位图格式深度解析:从1bit到32bit的存储奥秘与实战应用

1. BMP位图的前世今生 第一次接触BMP文件是在大学计算机图形学课上&#xff0c;当时教授拿着一个只有几十KB的黑白图标说&#xff1a;"这个小小的文件里藏着整个图形世界的密码。"这句话让我对BMP格式产生了浓厚兴趣。作为Windows系统的"元老级"图像格式&a…

作者头像 李华
网站建设 2026/4/19 19:54:23

Scapy实战:从ARP缓存投毒到中间人攻击的攻防演练

1. ARP协议与缓存投毒原理剖析 ARP&#xff08;Address Resolution Protocol&#xff09;是局域网通信的基础协议&#xff0c;它的作用就像现实生活中的电话簿&#xff0c;负责将IP地址转换成对应的MAC地址。每台设备都维护着一个ARP缓存表&#xff0c;记录着最近通信过的设备信…

作者头像 李华