Skip to content

Commit db2d323

Browse files
committed
store a static WebApplicationContext reference if the Spring jars get deployed in the same web application as the ContextLoader (SPR-5652)
1 parent 0598eaf commit db2d323

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

org.springframework.web/src/main/java/org/springframework/web/context/ContextLoader.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -141,13 +141,17 @@ public class ContextLoader {
141141

142142

143143
/**
144-
* Map from (thread context) ClassLoader to WebApplicationContext.
145-
* Often just holding one reference - if the ContextLoader class is
146-
* deployed in the web app ClassLoader itself!
144+
* Map from (thread context) ClassLoader to corresponding 'current' WebApplicationContext.
147145
*/
148146
private static final Map<ClassLoader, WebApplicationContext> currentContextPerThread =
149147
new ConcurrentHashMap<ClassLoader, WebApplicationContext>(1);
150148

149+
/**
150+
* The 'current' WebApplicationContext, if the ContextLoader class is
151+
* deployed in the web app ClassLoader itself.
152+
*/
153+
private static volatile WebApplicationContext currentContext;
154+
151155
/**
152156
* The root WebApplicationContext instance that this loader manages.
153157
*/
@@ -191,7 +195,14 @@ public WebApplicationContext initWebApplicationContext(ServletContext servletCon
191195
// it is available on ServletContext shutdown.
192196
this.context = createWebApplicationContext(servletContext, parent);
193197
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
194-
currentContextPerThread.put(Thread.currentThread().getContextClassLoader(), this.context);
198+
199+
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
200+
if (ccl == ContextLoader.class.getClassLoader()) {
201+
currentContext = this.context;
202+
}
203+
else if (ccl != null) {
204+
currentContextPerThread.put(ccl, this.context);
205+
}
195206

196207
if (logger.isDebugEnabled()) {
197208
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
@@ -364,7 +375,13 @@ public void closeWebApplicationContext(ServletContext servletContext) {
364375
}
365376
}
366377
finally {
367-
currentContextPerThread.remove(Thread.currentThread().getContextClassLoader());
378+
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
379+
if (ccl == ContextLoader.class.getClassLoader()) {
380+
currentContext = null;
381+
}
382+
else if (ccl != null) {
383+
currentContextPerThread.remove(ccl);
384+
}
368385
servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
369386
if (this.parentContextRef != null) {
370387
this.parentContextRef.release();
@@ -382,7 +399,14 @@ public void closeWebApplicationContext(ServletContext servletContext) {
382399
* @see org.springframework.web.context.support.SpringBeanAutowiringSupport
383400
*/
384401
public static WebApplicationContext getCurrentWebApplicationContext() {
385-
return currentContextPerThread.get(Thread.currentThread().getContextClassLoader());
402+
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
403+
if (ccl != null) {
404+
WebApplicationContext ccpt = currentContextPerThread.get(ccl);
405+
if (ccpt != null) {
406+
return ccpt;
407+
}
408+
}
409+
return currentContext;
386410
}
387411

388412
}

0 commit comments

Comments
 (0)