5

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Alisher
  • 480
  • 5
  • 16
  • PS Using spring boot 3.0.1 – Alisher Jan 07 '23 at 22:46
  • 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 anything – Anon Jan 07 '23 at 23:35
  • 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. – Alisher Jan 08 '23 at 09:27
  • ok i just tested and you just need to add Configuration annotation to TimeFormatConf class and it will work no problem – Anon Jan 08 '23 at 10:35
  • update: just put ConfigurationPropertiesScan annotation on the main class near the SpringBootApplication adding Configuration annotation was the old way of doing it my bad – Anon Jan 08 '23 at 11:15

1 Answers1

10

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Alisher
  • 480
  • 5
  • 16