Skip to content

Commit 0611b6d

Browse files
committed
Fix startup failure when WebFlux auto-config is skipped
Prior to this commit, the `HttpHandlerAutoConfiguration` would rely on the `WebFluxProperties` bean being present - this is most of the time true when the appplication is using the WebFlux auto-configuration. If the application is overriding the WebFlux auto-configuration and providing its own setup, the properties bean is not present and we should skip its usage. Fixes gh-20891
1 parent b6076f5 commit 0611b6d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.Map;
2121

22+
import org.springframework.beans.factory.ObjectProvider;
2223
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2324
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
2425
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -60,9 +61,10 @@ public AnnotationConfig(ApplicationContext applicationContext) {
6061
}
6162

6263
@Bean
63-
public HttpHandler httpHandler(WebFluxProperties properties) {
64+
public HttpHandler httpHandler(ObjectProvider<WebFluxProperties> propsProvider) {
6465
HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(this.applicationContext).build();
65-
if (StringUtils.hasText(properties.getBasePath())) {
66+
WebFluxProperties properties = propsProvider.getIfAvailable();
67+
if (properties != null && StringUtils.hasText(properties.getBasePath())) {
6668
Map<String, HttpHandler> handlersMap = Collections.singletonMap(properties.getBasePath(), httpHandler);
6769
return new ContextPathCompositeHandler(handlersMap);
6870
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import org.springframework.context.annotation.Configuration;
2626
import org.springframework.http.server.reactive.ContextPathCompositeHandler;
2727
import org.springframework.http.server.reactive.HttpHandler;
28+
import org.springframework.web.reactive.DispatcherHandler;
2829
import org.springframework.web.reactive.function.server.RouterFunction;
2930
import org.springframework.web.reactive.function.server.ServerResponse;
31+
import org.springframework.web.server.WebHandler;
3032

3133
import static org.assertj.core.api.Assertions.assertThat;
3234
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
@@ -58,6 +60,12 @@ void shouldConfigureHttpHandlerAnnotation() {
5860
.run((context) -> assertThat(context).hasSingleBean(HttpHandler.class));
5961
}
6062

63+
@Test
64+
void shouldConfigureHttpHandlerWithoutWebFluxAutoConfiguration() {
65+
this.contextRunner.withUserConfiguration(CustomWebHandler.class)
66+
.run((context) -> assertThat(context).hasSingleBean(HttpHandler.class));
67+
}
68+
6169
@Test
6270
void shouldConfigureBasePathCompositeHandler() {
6371
this.contextRunner.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class))
@@ -85,4 +93,14 @@ RouterFunction<ServerResponse> routerFunction() {
8593

8694
}
8795

96+
@Configuration(proxyBeanMethods = false)
97+
static class CustomWebHandler {
98+
99+
@Bean
100+
WebHandler webHandler() {
101+
return new DispatcherHandler();
102+
}
103+
104+
}
105+
88106
}

0 commit comments

Comments
 (0)