自定义Spring Boot 3 Starter无法创建ConfigurationProperties Bean
Spring Starter自动装配TimeFormatConf失败问题
我在自定义Spring Starter的Maven制品时,包含以下类与配置:
属性配置类
@ConfigurationProperties(prefix = "commons.time.formats") @Getter @Setter public class TimeFormatConf { // ... 字段省略 }
自动配置类
@Component @Configuration @AllArgsConstructor @EnableConfigurationProperties(TimeFormatConf.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) public class DateTimeConfigurer { private final TimeFormatConf timeFormatConf; @EventListener(ApplicationReadyEvent.class) public void configureTimeFormat() { TimeZone.setDefault(TimeZone.getTimeZone(timeFormatConf.getDynamicZone())); System.setProperty("user.timezone", timeFormatConf.getDynamicZone()); } }
spring.factories配置(路径:src/main/resources/META-INF/spring.factories)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ [package].DateTimeConfigurer,\ ...
问题现象
当尝试自动装配TimeFormatConf类时,Spring应用启动失败,报错信息如下:
Parameter 0 of constructor in com.some.package.b.Class required a bean
of type 'com.some.package.a.TimeFormatConf' that could not be found.
排查过程
制品已存在于类路径且配置正确,问题应与Spring的声明或配置逻辑有关。尝试过以下方案但均无效:
- 在
TimeFormatConf上添加@Component注解 - 在配置类上添加
@ComponentScan注解
临时解决方案
将项目回退到Spring Boot 2.7.7版本后,问题得以解决。初步判断这是Spring Boot新版本中的Bug或文档缺失问题,已向Spring团队提交相关Issue。
内容的提问来源于stack exchange,提问作者Alisher
相关产品推荐
相关产品推荐

