Skip to content

Commit c7d2799

Browse files
committed
Add configuration property for DispatcherServlet event publishing
Closes gh-17500
1 parent afe3ab7 commit c7d2799

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public DispatcherServlet dispatcherServlet(HttpProperties httpProperties, WebMvc
9292
dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest());
9393
dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest());
9494
dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound());
95+
dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents());
9596
dispatcherServlet.setEnableLoggingRequestDetails(httpProperties.isLogRequestDetails());
9697
return dispatcherServlet;
9798
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public class WebMvcProperties {
7676
*/
7777
private boolean ignoreDefaultModelOnRedirect = true;
7878

79+
/**
80+
* Whether to publish a ServletRequestHandledEvent at the end of each request.
81+
*/
82+
private boolean publishRequestHandledEvents = true;
83+
7984
/**
8085
* Whether a "NoHandlerFoundException" should be thrown if no Handler was found to
8186
* process a request.
@@ -143,6 +148,14 @@ public void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect
143148
this.ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect;
144149
}
145150

151+
public boolean isPublishRequestHandledEvents() {
152+
return this.publishRequestHandledEvents;
153+
}
154+
155+
public void setPublishRequestHandledEvents(boolean publishRequestHandledEvents) {
156+
this.publishRequestHandledEvents = publishRequestHandledEvents;
157+
}
158+
146159
public boolean isThrowExceptionIfNoHandlerFound() {
147160
return this.throwExceptionIfNoHandlerFound;
148161
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfigurationTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,19 @@ void dispatcherServletDefaultConfig() {
149149
assertThat(dispatcherServlet).extracting("dispatchOptionsRequest").containsExactly(true);
150150
assertThat(dispatcherServlet).extracting("dispatchTraceRequest").containsExactly(false);
151151
assertThat(dispatcherServlet).extracting("enableLoggingRequestDetails").containsExactly(false);
152+
assertThat(dispatcherServlet).extracting("publishEvents").containsExactly(true);
152153
assertThat(context.getBean("dispatcherServletRegistration")).hasFieldOrPropertyWithValue("loadOnStartup",
153154
-1);
154155
});
155156
}
156157

157158
@Test
158159
void dispatcherServletCustomConfig() {
159-
this.contextRunner.withPropertyValues("spring.mvc.throw-exception-if-no-handler-found:true",
160-
"spring.mvc.dispatch-options-request:false", "spring.mvc.dispatch-trace-request:true",
161-
"spring.mvc.servlet.load-on-startup=5").run((context) -> {
160+
this.contextRunner
161+
.withPropertyValues("spring.mvc.throw-exception-if-no-handler-found:true",
162+
"spring.mvc.dispatch-options-request:false", "spring.mvc.dispatch-trace-request:true",
163+
"spring.mvc.publish-request-handled-events:false", "spring.mvc.servlet.load-on-startup=5")
164+
.run((context) -> {
162165
DispatcherServlet dispatcherServlet = context.getBean(DispatcherServlet.class);
163166
assertThat(dispatcherServlet).extracting("throwExceptionIfNoHandlerFound").containsExactly(true);
164167
assertThat(dispatcherServlet).extracting("dispatchOptionsRequest").containsExactly(false);

0 commit comments

Comments
 (0)