Skip to content

Commit b1f5f51

Browse files
committed
Synchronized onRefresh execution for concurrent ContextRefreshedEvent
Issue: SPR-17442
1 parent 2ce03f3 commit b1f5f51

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
209209
/** Should we dispatch an HTTP TRACE request to {@link #doService}?. */
210210
private boolean dispatchTraceRequest = false;
211211

212+
/** Whether to log potentially sensitive info (request params at DEBUG + headers at TRACE). */
213+
private boolean enableLoggingRequestDetails = false;
214+
212215
/** WebApplicationContext for this servlet. */
213216
@Nullable
214217
private WebApplicationContext webApplicationContext;
@@ -217,10 +220,10 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
217220
private boolean webApplicationContextInjected = false;
218221

219222
/** Flag used to detect whether onRefresh has already been called. */
220-
private boolean refreshEventReceived = false;
223+
private volatile boolean refreshEventReceived = false;
221224

222-
/** Whether to log potentially sensitive info (request params at DEBUG + headers at TRACE). */
223-
private boolean enableLoggingRequestDetails = false;
225+
/** Monitor for synchronized onRefresh execution. */
226+
private final Object onRefreshMonitor = new Object();
224227

225228

226229
/**
@@ -591,7 +594,9 @@ protected WebApplicationContext initWebApplicationContext() {
591594
// Either the context is not a ConfigurableApplicationContext with refresh
592595
// support or the context injected at construction time had already been
593596
// refreshed -> trigger initial onRefresh manually here.
594-
onRefresh(wac);
597+
synchronized (this.onRefreshMonitor) {
598+
onRefresh(wac);
599+
}
595600
}
596601

597602
if (this.publishContext) {
@@ -832,7 +837,9 @@ public void refresh() {
832837
*/
833838
public void onApplicationEvent(ContextRefreshedEvent event) {
834839
this.refreshEventReceived = true;
835-
onRefresh(event.getApplicationContext());
840+
synchronized (this.onRefreshMonitor) {
841+
onRefresh(event.getApplicationContext());
842+
}
836843
}
837844

838845
/**

0 commit comments

Comments
 (0)