Browse Source

Synchronized onRefresh execution for concurrent ContextRefreshedEvent

Issue: SPR-17442
pull/2011/head
Juergen Hoeller 6 years ago
parent
commit
b1f5f51503
  1. 13
      spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java

13
spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java

@ -209,6 +209,9 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
/** Should we dispatch an HTTP TRACE request to {@link #doService}?. */ /** Should we dispatch an HTTP TRACE request to {@link #doService}?. */
private boolean dispatchTraceRequest = false; private boolean dispatchTraceRequest = false;
/** Whether to log potentially sensitive info (request params at DEBUG + headers at TRACE). */
private boolean enableLoggingRequestDetails = false;
/** WebApplicationContext for this servlet. */ /** WebApplicationContext for this servlet. */
@Nullable @Nullable
private WebApplicationContext webApplicationContext; private WebApplicationContext webApplicationContext;
@ -217,10 +220,10 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
private boolean webApplicationContextInjected = false; private boolean webApplicationContextInjected = false;
/** Flag used to detect whether onRefresh has already been called. */ /** Flag used to detect whether onRefresh has already been called. */
private boolean refreshEventReceived = false; private volatile boolean refreshEventReceived = false;
/** Whether to log potentially sensitive info (request params at DEBUG + headers at TRACE). */ /** Monitor for synchronized onRefresh execution. */
private boolean enableLoggingRequestDetails = false; private final Object onRefreshMonitor = new Object();
/** /**
@ -591,8 +594,10 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
// Either the context is not a ConfigurableApplicationContext with refresh // Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been // support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here. // refreshed -> trigger initial onRefresh manually here.
synchronized (this.onRefreshMonitor) {
onRefresh(wac); onRefresh(wac);
} }
}
if (this.publishContext) { if (this.publishContext) {
// Publish the context as a servlet context attribute. // Publish the context as a servlet context attribute.
@ -832,8 +837,10 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
*/ */
public void onApplicationEvent(ContextRefreshedEvent event) { public void onApplicationEvent(ContextRefreshedEvent event) {
this.refreshEventReceived = true; this.refreshEventReceived = true;
synchronized (this.onRefreshMonitor) {
onRefresh(event.getApplicationContext()); onRefresh(event.getApplicationContext());
} }
}
/** /**
* Template method which can be overridden to add servlet-specific refresh work. * Template method which can be overridden to add servlet-specific refresh work.

Loading…
Cancel
Save