Skip to content

Commit c86a7c7

Browse files
committed
Exclude ReactorAutoConfiguration from global lazy init
This commits registers LazyInitializationExcludeFilter to exclude `ReactorAutoConfiguration` from global lazy init when 'spring.reactor.context-propagation' is set to 'auto'. Signed-off-by: Dmytro Nosan <[email protected]>
1 parent a553327 commit c86a7c7

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/reactor/ReactorAutoConfiguration.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,10 +18,13 @@
1818

1919
import reactor.core.publisher.Hooks;
2020

21+
import org.springframework.boot.LazyInitializationExcludeFilter;
2122
import org.springframework.boot.autoconfigure.AutoConfiguration;
2223
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2426
import org.springframework.boot.context.properties.EnableConfigurationProperties;
27+
import org.springframework.context.annotation.Bean;
2528

2629
/**
2730
* {@link EnableAutoConfiguration Auto-configuration} for Reactor.
@@ -40,4 +43,10 @@ public class ReactorAutoConfiguration {
4043
}
4144
}
4245

46+
@Bean
47+
@ConditionalOnProperty(name = "spring.reactor.context-propagation", havingValue = "auto")
48+
static LazyInitializationExcludeFilter reactorLazyInitializationExcludeFilter() {
49+
return LazyInitializationExcludeFilter.forBeanTypes(ReactorAutoConfiguration.class);
50+
}
51+
4352
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/reactor/ReactorAutoConfigurationTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,8 @@
2828
import reactor.core.publisher.Mono;
2929
import reactor.util.context.Context;
3030

31+
import org.springframework.boot.LazyInitializationBeanFactoryPostProcessor;
32+
import org.springframework.boot.LazyInitializationExcludeFilter;
3133
import org.springframework.boot.autoconfigure.AutoConfigurations;
3234
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3335

@@ -75,6 +77,7 @@ void shouldNotConfigurePropagationByDefault() {
7577
.contextWrite(Context.of(THREADLOCAL_KEY, "updated"))
7678
.block();
7779
assertThat(threadLocalValue.get()).isEqualTo("initial");
80+
assertThat(applicationContext).doesNotHaveBean(LazyInitializationExcludeFilter.class);
7881
});
7982
}
8083

@@ -90,4 +93,19 @@ void shouldConfigurePropagationIfSetToAuto() {
9093
});
9194
}
9295

96+
@Test
97+
void shouldConfigurePropagationIfSetToAutoAndLazyInitializationIsEnabled() {
98+
AtomicReference<String> threadLocalValue = new AtomicReference<>();
99+
this.contextRunner.withPropertyValues("spring.reactor.context-propagation=AUTO")
100+
.withInitializer((applicationContext) -> applicationContext
101+
.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor()))
102+
.run((applicationContext) -> {
103+
Mono.just("test")
104+
.doOnNext((element) -> threadLocalValue.set(THREADLOCAL_VALUE.get()))
105+
.contextWrite(Context.of(THREADLOCAL_KEY, "updated"))
106+
.block();
107+
assertThat(threadLocalValue.get()).isEqualTo("updated");
108+
});
109+
}
110+
93111
}

0 commit comments

Comments
 (0)