Add configuration property for DispatcherServlet event publishing

Closes gh-17500
pull/17591/head
Andy Wilkinson 5 years ago
parent afe3ab7517
commit c7d2799f4e

@ -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;
}

@ -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;
}

@ -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);

Loading…
Cancel
Save