From cc4a1af09182978dc32d333e44e0e8229a89bc3b Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 23 Nov 2020 09:56:02 +0000 Subject: [PATCH] Ensure AsyncSupportConfigurer is created once Now that we have two adapters that need access to the configurer, we need to save it in a field like others. See gh-25931 --- .../WebMvcConfigurationSupport.java | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index a87b57d5a8..ae40c3dc9f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -253,6 +253,9 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv @Nullable private Map corsConfigurations; + @Nullable + private AsyncSupportConfigurer asyncSupportConfigurer; + /** * Set the Spring {@link ApplicationContext}, e.g. for resource loading. @@ -652,8 +655,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv adapter.setResponseBodyAdvice(Collections.singletonList(new JsonViewResponseBodyAdvice())); } - AsyncSupportConfigurer configurer = new AsyncSupportConfigurer(); - configureAsyncSupport(configurer); + AsyncSupportConfigurer configurer = getAsyncSupportConfigurer(); if (configurer.getTaskExecutor() != null) { adapter.setTaskExecutor(configurer.getTaskExecutor()); } @@ -684,8 +686,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv public HandlerFunctionAdapter handlerFunctionAdapter() { HandlerFunctionAdapter adapter = new HandlerFunctionAdapter(); - AsyncSupportConfigurer configurer = new AsyncSupportConfigurer(); - configureAsyncSupport(configurer); + AsyncSupportConfigurer configurer = getAsyncSupportConfigurer(); if (configurer.getTimeout() != null) { adapter.setAsyncRequestTimeout(configurer.getTimeout()); } @@ -717,13 +718,6 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv return null; } - /** - * Override this method to configure asynchronous request processing options. - * @see AsyncSupportConfigurer - */ - protected void configureAsyncSupport(AsyncSupportConfigurer configurer) { - } - /** * Return a {@link FormattingConversionService} for use with annotated controllers. *

See {@link #addFormatters} as an alternative to overriding this method. @@ -945,6 +939,26 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv } } + /** + * Callback for building the {@link AsyncSupportConfigurer}. + * Delegates to {@link #configureAsyncSupport(AsyncSupportConfigurer)}. + * @since 5.3.2 + */ + protected AsyncSupportConfigurer getAsyncSupportConfigurer() { + if (this.asyncSupportConfigurer == null) { + this.asyncSupportConfigurer = new AsyncSupportConfigurer(); + configureAsyncSupport(this.asyncSupportConfigurer); + } + return this.asyncSupportConfigurer; + } + + /** + * Override this method to configure asynchronous request processing options. + * @see AsyncSupportConfigurer + */ + protected void configureAsyncSupport(AsyncSupportConfigurer configurer) { + } + /** * Return an instance of {@link CompositeUriComponentsContributor} for use with * {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}.