From c7d2799f4e1b42daa96ea1c1190612eb0627039e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 19 Jul 2019 13:11:19 +0100 Subject: [PATCH] Add configuration property for DispatcherServlet event publishing Closes gh-17500 --- .../servlet/DispatcherServletAutoConfiguration.java | 1 + .../autoconfigure/web/servlet/WebMvcProperties.java | 13 +++++++++++++ .../DispatcherServletAutoConfigurationTests.java | 9 ++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration.java index a2cc94599b..e84453ec75 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration.java @@ -92,6 +92,7 @@ public class DispatcherServletAutoConfiguration { dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest()); dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest()); dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound()); + dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents()); dispatcherServlet.setEnableLoggingRequestDetails(httpProperties.isLogRequestDetails()); return dispatcherServlet; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.java index b0f665f156..da6e0ee7b6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.java @@ -76,6 +76,11 @@ public class WebMvcProperties { */ private boolean ignoreDefaultModelOnRedirect = true; + /** + * Whether to publish a ServletRequestHandledEvent at the end of each request. + */ + private boolean publishRequestHandledEvents = true; + /** * Whether a "NoHandlerFoundException" should be thrown if no Handler was found to * process a request. @@ -143,6 +148,14 @@ public class WebMvcProperties { this.ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect; } + public boolean isPublishRequestHandledEvents() { + return this.publishRequestHandledEvents; + } + + public void setPublishRequestHandledEvents(boolean publishRequestHandledEvents) { + this.publishRequestHandledEvents = publishRequestHandledEvents; + } + public boolean isThrowExceptionIfNoHandlerFound() { return this.throwExceptionIfNoHandlerFound; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfigurationTests.java index 7c086bd8c7..0c920c2b65 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfigurationTests.java @@ -149,6 +149,7 @@ class DispatcherServletAutoConfigurationTests { assertThat(dispatcherServlet).extracting("dispatchOptionsRequest").containsExactly(true); assertThat(dispatcherServlet).extracting("dispatchTraceRequest").containsExactly(false); assertThat(dispatcherServlet).extracting("enableLoggingRequestDetails").containsExactly(false); + assertThat(dispatcherServlet).extracting("publishEvents").containsExactly(true); assertThat(context.getBean("dispatcherServletRegistration")).hasFieldOrPropertyWithValue("loadOnStartup", -1); }); @@ -156,9 +157,11 @@ class DispatcherServletAutoConfigurationTests { @Test void dispatcherServletCustomConfig() { - this.contextRunner.withPropertyValues("spring.mvc.throw-exception-if-no-handler-found:true", - "spring.mvc.dispatch-options-request:false", "spring.mvc.dispatch-trace-request:true", - "spring.mvc.servlet.load-on-startup=5").run((context) -> { + this.contextRunner + .withPropertyValues("spring.mvc.throw-exception-if-no-handler-found:true", + "spring.mvc.dispatch-options-request:false", "spring.mvc.dispatch-trace-request:true", + "spring.mvc.publish-request-handled-events:false", "spring.mvc.servlet.load-on-startup=5") + .run((context) -> { DispatcherServlet dispatcherServlet = context.getBean(DispatcherServlet.class); assertThat(dispatcherServlet).extracting("throwExceptionIfNoHandlerFound").containsExactly(true); assertThat(dispatcherServlet).extracting("dispatchOptionsRequest").containsExactly(false);