Skip to content

Commit 88f4e92

Browse files
committed
Synchronized onRefresh execution for concurrent ContextRefreshedEvent
Issue: SPR-17442 (cherry picked from commit b1f5f51)
1 parent 591e7f1 commit 88f4e92

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
213213
private boolean webApplicationContextInjected = false;
214214

215215
/** Flag used to detect whether onRefresh has already been called */
216-
private boolean refreshEventReceived = false;
216+
private volatile boolean refreshEventReceived = false;
217+
218+
/** Monitor for synchronized onRefresh execution */
219+
private final Object onRefreshMonitor = new Object();
217220

218221

219222
/**
@@ -490,8 +493,8 @@ public void setApplicationContext(ApplicationContext applicationContext) {
490493
@Override
491494
protected final void initServletBean() throws ServletException {
492495
getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
493-
if (this.logger.isInfoEnabled()) {
494-
this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
496+
if (logger.isInfoEnabled()) {
497+
logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
495498
}
496499
long startTime = System.currentTimeMillis();
497500

@@ -500,13 +503,13 @@ protected final void initServletBean() throws ServletException {
500503
initFrameworkServlet();
501504
}
502505
catch (ServletException | RuntimeException ex) {
503-
this.logger.error("Context initialization failed", ex);
506+
logger.error("Context initialization failed", ex);
504507
throw ex;
505508
}
506509

507-
if (this.logger.isInfoEnabled()) {
510+
if (logger.isInfoEnabled()) {
508511
long elapsedTime = System.currentTimeMillis() - startTime;
509-
this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
512+
logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
510513
elapsedTime + " ms");
511514
}
512515
}
@@ -558,7 +561,9 @@ protected WebApplicationContext initWebApplicationContext() {
558561
// Either the context is not a ConfigurableApplicationContext with refresh
559562
// support or the context injected at construction time had already been
560563
// refreshed -> trigger initial onRefresh manually here.
561-
onRefresh(wac);
564+
synchronized (this.onRefreshMonitor) {
565+
onRefresh(wac);
566+
}
562567
}
563568

564569
if (this.publishContext) {
@@ -808,7 +813,9 @@ public void refresh() {
808813
*/
809814
public void onApplicationEvent(ContextRefreshedEvent event) {
810815
this.refreshEventReceived = true;
811-
onRefresh(event.getApplicationContext());
816+
synchronized (this.onRefreshMonitor) {
817+
onRefresh(event.getApplicationContext());
818+
}
812819
}
813820

814821
/**

0 commit comments

Comments
 (0)