前面两步和自动配置的骨架都没问题,但第三步缺了最关键的一环:自动配置类必须在 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 里登记(一行一个全限定类名),否则写得再标准也不会生效——这是「starter 写了但没反应」的头号原因。3.0 起 spring.factories 里的 EnableAutoConfiguration 键已彻底不再支持,2.7 就弃用了,升级项目里残留的老文件记得删掉。
再补三个细节。一是配置属性类的绑定:Boot 3 里只有一个构造器时 @ConstructorBinding 可以省略,但记得在 autoconfigure 模块引入 spring-boot-configuration-processor(<optional>true</optional>),否则不会生成 spring-configuration-metadata.json,用户在 IDE 里敲 my.xxx 没有任何提示,体验差一截。二是自动配置之间的顺序,别靠猜 bean 注册顺序,用 @AutoConfigureAfter/Before 显式声明,条件注解只负责「要不要装配」,不负责「谁先装配」。三是每个自动配置类都该配一个 ApplicationContextRunner 单测,这是官方 starter 的标配,能拦住大部分条件写错的低级问题。
监控这块建议按生产三件套接:spring-boot-starter-actuator + micrometer-registry-prometheus,暴露 management.endpoints.web.exposure.include=health,info,prometheus,metrics;健康检查给 K8s 用要开 management.endpoint.health.probes.enabled=true,拆出 liveness/readiness 两个组,show-details 设成 when-authorized 别裸奔。链路追踪上 Boot 3 已经用 Micrometer Tracing 取代了 Sleuth,想让它自动进日志,除了 micrometer-tracing-bridge-brave 还要在 logback pattern 里手动加上 %X{traceId}。再加一句 server.shutdown=graceful 配合 spring.lifecycle.timeout-per-shutdown-phase=30s,滚动发布时才不会把在途请求砍掉。
最后一个坑:starter 聚合模块里千万别引 spring-boot-starter-web,会把 Web 依赖强塞给非 Web 应用;第三方依赖统一在 autoconfigure 模块标 optional,聚合模块只做「拼装」。Actuator 端点记得走 management.server.port 或 Spring Security 单独收口,默认端口公开等于把内存和 bean 列表挂公网上了。
正式会员
认证极客






