1
1
/*
2
- * Copyright 2002-2009 the original author or authors.
2
+ * Copyright 2002-2010 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -141,13 +141,17 @@ public class ContextLoader {
141
141
142
142
143
143
/**
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.
147
145
*/
148
146
private static final Map <ClassLoader , WebApplicationContext > currentContextPerThread =
149
147
new ConcurrentHashMap <ClassLoader , WebApplicationContext >(1 );
150
148
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
+
151
155
/**
152
156
* The root WebApplicationContext instance that this loader manages.
153
157
*/
@@ -191,7 +195,14 @@ public WebApplicationContext initWebApplicationContext(ServletContext servletCon
191
195
// it is available on ServletContext shutdown.
192
196
this .context = createWebApplicationContext (servletContext , parent );
193
197
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
+ }
195
206
196
207
if (logger .isDebugEnabled ()) {
197
208
logger .debug ("Published root WebApplicationContext as ServletContext attribute with name [" +
@@ -364,7 +375,13 @@ public void closeWebApplicationContext(ServletContext servletContext) {
364
375
}
365
376
}
366
377
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
+ }
368
385
servletContext .removeAttribute (WebApplicationContext .ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE );
369
386
if (this .parentContextRef != null ) {
370
387
this .parentContextRef .release ();
@@ -382,7 +399,14 @@ public void closeWebApplicationContext(ServletContext servletContext) {
382
399
* @see org.springframework.web.context.support.SpringBeanAutowiringSupport
383
400
*/
384
401
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 ;
386
410
}
387
411
388
412
}
0 commit comments