手写一个最小 Starter:从 0 到能看懂

张开发
2026/4/20 1:41:24 15 分钟阅读

分享文章

手写一个最小 Starter:从 0 到能看懂
一、我们先定目标我们做一个最简单的 starter名字叫ark-hello-starter功能非常简单用户只要引入这个 starter就能直接注入一个HelloService来调用。像这样Autowired private HelloService helloService;然后System.out.println(helloService.sayHello());输出hello from ark starter二、你先理解starter 到底拆成什么最小 starter核心就 3 样东西1一个普通业务类比如HelloService2一个自动配置类比如HelloAutoConfiguration3一个配置注册文件告诉 Spring Boot“启动时把我的自动配置类也拿去加载”三、整体结构先看懂你可以先把 starter 理解成这个目录ark-hello-starter ├── src/main/java │ └── com/ark/starter │ ├── service │ │ └── HelloService.java │ └── autoconfigure │ └── HelloAutoConfiguration.java └── src/main/resources └── META-INF/spring └── org.springframework.boot.autoconfigure.AutoConfiguration.imports四、第一步写一个最普通的服务类这个类本身一点都不神秘就是个普通 Java 类package com.ark.starter.service; public class HelloService { public String sayHello() { return hello from ark starter; } }五、第二步写自动配置类这一步才是 starter 的关键。package com.ark.starter.autoconfigure; import com.ark.starter.service.HelloService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class HelloAutoConfiguration { Bean public HelloService helloService() { return new HelloService(); } }你先别管复杂的条件注解。先只看本质自动配置类 启动时帮你把 Bean 放进 Spring 容器也就是以前你要手动写Configuration public class MyConfig { Bean public HelloService helloService() { return new HelloService(); } }现在 starter 帮你写好了。六、第三步注册自动配置类这一步最重要也是很多人第一次最陌生的地方。在这个文件里写src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports内容com.ark.starter.autoconfigure.HelloAutoConfiguration这一句的意思就是“Spring Boot 启动时请把这个自动配置类也加载进来。”七、到这里 starter 就成了你现在回头看其实 starter 没有你想象中那么玄学。本质就是普通类 自动配置类 自动配置注册文件八、再升级一步加上 ConditionalOnClass现在我们把你前面学的东西塞进来。比如改成这样package com.ark.starter.autoconfigure; import com.ark.starter.service.HelloService; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration ConditionalOnClass(HelloService.class) public class HelloAutoConfiguration { Bean public HelloService helloService() { return new HelloService(); } }虽然这个例子里HelloService.class本来就在 starter 里条件判断意义不大但它是为了让你先看懂这个形式。它表达的是只有类存在配置才生效。企业里常见的是判断第三方依赖比如ConditionalOnClass(RedisTemplate.class)意思就是只有项目里引入了 Redis 相关类我才自动配置 Redis 功能。九、再升级一步避免用户自己定义了同名 Bean再加一个很常见的注解ConditionalOnMissingBean改成这样package com.ark.starter.autoconfigure; 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() { return new HelloService(); } }这句话的意思如果用户自己没定义HelloService我才给他自动创建。这个非常重要因为 starter 不能太霸道。十、现在你彻底理解一下运行流程用户项目一启动Spring Boot 干了这些事1. 启动应用2. 读取 AutoConfiguration.imports3. 发现 HelloAutoConfiguration4. 判断条件是否满足5. 满足则执行 Bean6. 把 HelloService 放入容器7. 用户就能直接 Autowired这就是 starter 的本质流程。十一、用户项目怎么用这个 starter假设你已经把ark-hello-starter打成 jar并安装到本地仓库。用户项目里只要加依赖dependency groupIdcom.ark/groupId artifactIdark-hello-starter/artifactId version1.0.0/version /dependency然后直接写package com.demo.controller; import com.ark.starter.service.HelloService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; 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返回hello from ark starter十二、你现在会发现一件事starter 其实不是“某种神秘技术”它只是把别人本来要手动写的配置提前封装好了。所以它的价值是降低接入成本统一技术方案自动完成初始化做成可插拔能力十三、和 Android 经验怎么对齐这块其实很好理解。你可以把 starter 类比成Android 里的 SDK 接入包比如你引一个网络库 SDKimplementation xxx-sdk然后它可能自动帮你提供 API Client提供拦截器提供初始化逻辑提供默认配置Spring Boot starter 也是这个意思只不过它用的是依赖自动配置Spring Bean 容器所以你可以把它理解成后端里的“自动初始化 SDK 包”十四、为什么 starter 往往要拆成两部分你以后会看到一些文章说xxx-spring-boot-starterxxx-spring-boot-autoconfigure这是因为企业里经常把它拆开。1starter负责引依赖2autoconfigure负责自动配置你现在先不用纠结这个拆分。你现阶段只要记住starter 核心一定绕不开自动配置类。十五、给你一个最小可跑版本我直接给你一套最简代码骨架你以后回头抄都行。1HelloService.javapackage com.ark.starter.service; public class HelloService { public String sayHello() { return hello from ark starter; } }2HelloAutoConfiguration.javapackage com.ark.starter.autoconfigure; 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() { return new HelloService(); } }3AutoConfiguration.importscom.ark.starter.autoconfigure.HelloAutoConfiguration路径必须是src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports十六、你现在最容易混乱的点我直接帮你拆掉误区1starter 是不是特殊语法不是。它本质上还是普通类配置类注册文件误区2没有 ConditionalOnClass 就不是 starter不是。没有条件注解也能是 starter。只是企业里一般都会加条件避免乱加载。误区3starter 一定很复杂不是。最小 starter 非常简单。复杂的是企业级 starter会加上配置参数绑定条件判断默认值多实现切换日志扩展点十七、该掌握到什么程度你现在不用追求“会写企业级 starter”。你只要先到这一步就够了第一层知道 starter 是什么第二层知道 starter 由什么组成第三层知道自动配置类怎么把 Bean 放进去第四层知道ConditionalOnClass和ConditionalOnMissingBean为什么常出现到这里你就已经不是“starter 小白”了。十八、最短记忆我给你一句最短记忆法starter 依赖入口 自动配置再展开一点starter 帮用户自动把某套功能接进 Spring 容器十九、最后我帮你把这件事落到你当前阶段现在不用急着真的去写一个完整 starter 发到项目里。现在最重要的是先把这个骨架理解了以后看到spring-boot-starter-web、redis starter、mybatis starter你脑子里就知道它们在干嘛。这就已经值了。下一篇《手写 Starter 进阶ConfigurationProperties 实战支持 application.yml》这个一接上你就会明白为什么很多 starter 可以这样用ark:hello:enabled: truemessage: hello world这一步就会更像真正企业里的 starter。

更多文章