自定义 Spring Boot 3 Starter 不创建 ConfigurationProperties bean

Ali*_*her 12 java spring-boot spring-boot-starter configurationproperties

我正在自定义 Spring Starter 项目中使用以下类和配置创建 Maven 工件:

@ConfigurationProperties(prefix = "commons.time.formats")
@Getter
@Setter
public class TimeFormatConf {
... fields
}
Run Code Online (Sandbox Code Playgroud)
@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());
  }
}
Run Code Online (Sandbox Code Playgroud)

还有 spring.factories( src/main/resources/META-INF/spring.factories):

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
[package].DateTimeConfigurer,\
...
Run Code Online (Sandbox Code Playgroud)

当我尝试自动装配 TimeFormatConf 类时,我的 spring 应用程序在启动时崩溃,因为它找不到 TimeFormatConf 类的 bean:

com.some.package.b.Class 中构造函数的参数 0 需要类型为“com.some.package.a.TimeFormatConf”的 bean,但无法找到。

该工件在类路径上具有正确的配置。所以它必须对 Spring 的声明或配置做一些事情。

我还尝试添加@Component属性类和@ComponentScan配置类没有任何效果。

在两个项目上恢复到 Spring Boot 2.7.7 解决了这个问题。所以这似乎是一个错误或至少缺乏文档。我已经提出了一个问题来与 Spring 团队跟进:https://github.com/spring-projects/spring-boot/issues/33720

Ali*_*her 27

自 2.7 版本以来,Spring 检测配置 bean 的方式似乎发生了变化,本迁移指南似乎涵盖了这一点

META-INF/spring.factories基本上,您不需要在 中提供配置,而是在META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

  • 对于 Spring Boot 3.xx,这是一个重大变化。不支持向后兼容“META-INF/spring.factories”。 (3认同)