spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Setting spring.reactor.context-propagation has no effect when lazy initialization is enabled

Open Akaame opened this issue 7 months ago • 2 comments

Setting spring.main.lazy-initialization: true effectively disables the spring.reactor.context-propagation: AUTO, as the ReactorAutoConfiguration^1 is never instantiated during the application startup. Turning on eager initialization alleviates this problem, which leads me to believe that this particular setup was overlooked when the AutoConfiguration class what added. Hence, I created this issue, even though the text reads more like a question.

We are currently doing the following

@AutoConfiguration
@ConditionalOnClass(Hooks.class)
@EnableConfigurationProperties(ReactorProperties.class)
@Lazy(false)
public class EagerReactorAutoConfiguration {
  EagerReactorAutoConfiguration(ReactorProperties properties) {
    if (properties.getContextPropagation() == ReactorProperties.ContextPropagationMode.AUTO) {
      Hooks.enableAutomaticContextPropagation();
    }
  }
}

This is not elegant at all and I am not super sure if there is a way of forcing eager auto configuration for such cases (or if this should be rather fixed at Spring Boot level).

Cheers!

Akaame avatar Jun 06 '25 13:06 Akaame

You're right, this is a bug. Until it's fixed, you can use a filter to exclude the ReactorAutoConfiguration bean from lazy init:

@Bean
LazyInitializationExcludeFilter eagerReactorAutoConfiguration() {
	return LazyInitializationExcludeFilter.forBeanTypes(ReactorAutoConfiguration.class);
}

wilkinsona avatar Jun 06 '25 13:06 wilkinsona

That worked out perfectly for our setup. Thanks for the quick answer @wilkinsona 🍺

Akaame avatar Jun 06 '25 14:06 Akaame

We discussed this today and we've decided to add the LazyInitializationExcludeFilter directly in our auto-configuration. For 4.x we'll try and change the static calls. I've opened #46014 to deal with that.

philwebb avatar Jun 18 '25 15:06 philwebb