Alisher
Alisher

Reputation: 940

Custom Spring Boot 3 Starter does not create ConfigurationProperties beans

I am creating a Maven artifact with following classes and configuration in custom Spring Starter project:

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

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

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
[package].DateTimeConfigurer,\
...

When I try to autowire a TimeFormatConf class my spring application crashes at start up as it cannot find a bean of TimeFormatConf class:

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.

The artifact is present with correct configuration on classpath. So it has to do something with either declaration or configuration of Spring.

I have also tried adding @Component on properties class and @ComponentScan on configuration class does not have any effect.

Reverting to Spring Boot 2.7.7 on both projects fixed the problem. So it seems to be a bug or at least lack of documentation. I have opened an issue to follow this up with spring team: https://github.com/spring-projects/spring-boot/issues/33720

Upvotes: 17

Views: 15896

Answers (2)

Vladislav Miltsyn
Vladislav Miltsyn

Reputation: 23

By the way, as a workaround you could use @Import(DateTimeConfigurer.class) annotation

Upvotes: 0

Alisher
Alisher

Reputation: 940

There seems to be a change in the way configurations beans are detected by Spring since version 2.7, it seems to be covered in this migration guide

Basically, instead of providing configuration in META-INF/spring.factories, you would do so in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

Upvotes: 39

Related Questions