Custom Spring Boot 3 Starter does not create ConfigurationProperties beans
Asked Answered
J

1

15

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

Johnston answered 7/1, 2023 at 22:45 Comment(5)
PS Using spring boot 3.0.1Johnston
try adding the NoArgsConstructor on the class TimeFormatConf but with the code you have now it should be working maybe its something with the new version of spring also use RequiredArgsConstructor annotation if you are using final also you can comment Configuration and EnableConfigurationProperties(TimeFormatConf.class) in DateTimeConfigurer i dont think they are doing anythingLovieloving
Well, I have tried adding those, but it does not make a difference. Also, properties are bound through setters unless you specify constructor binding annotation.Johnston
ok i just tested and you just need to add Configuration annotation to TimeFormatConf class and it will work no problemLovieloving
update: just put ConfigurationPropertiesScan annotation on the main class near the SpringBootApplication adding Configuration annotation was the old way of doing it my badLovieloving
J
37

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

Johnston answered 15/1, 2023 at 11:30 Comment(1)
With spring boot 3.x.x this a breaking change. backward compatibility with META-INF/spring.factories is not supported.Hulky

© 2022 - 2024 — McMap. All rights reserved.