store a static WebApplicationContext reference if the Spring jars get deployed in the same web application as the ContextLoader (SPR-5652)
This commit is contained in:
parent
0598eafb84
commit
db2d323d96
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2010 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -141,13 +141,17 @@ public class ContextLoader {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map from (thread context) ClassLoader to WebApplicationContext.
|
* Map from (thread context) ClassLoader to corresponding 'current' WebApplicationContext.
|
||||||
* Often just holding one reference - if the ContextLoader class is
|
|
||||||
* deployed in the web app ClassLoader itself!
|
|
||||||
*/
|
*/
|
||||||
private static final Map<ClassLoader, WebApplicationContext> currentContextPerThread =
|
private static final Map<ClassLoader, WebApplicationContext> currentContextPerThread =
|
||||||
new ConcurrentHashMap<ClassLoader, WebApplicationContext>(1);
|
new ConcurrentHashMap<ClassLoader, WebApplicationContext>(1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The 'current' WebApplicationContext, if the ContextLoader class is
|
||||||
|
* deployed in the web app ClassLoader itself.
|
||||||
|
*/
|
||||||
|
private static volatile WebApplicationContext currentContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The root WebApplicationContext instance that this loader manages.
|
* The root WebApplicationContext instance that this loader manages.
|
||||||
*/
|
*/
|
||||||
|
|
@ -191,7 +195,14 @@ public class ContextLoader {
|
||||||
// it is available on ServletContext shutdown.
|
// it is available on ServletContext shutdown.
|
||||||
this.context = createWebApplicationContext(servletContext, parent);
|
this.context = createWebApplicationContext(servletContext, parent);
|
||||||
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
|
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
|
||||||
currentContextPerThread.put(Thread.currentThread().getContextClassLoader(), this.context);
|
|
||||||
|
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
|
||||||
|
if (ccl == ContextLoader.class.getClassLoader()) {
|
||||||
|
currentContext = this.context;
|
||||||
|
}
|
||||||
|
else if (ccl != null) {
|
||||||
|
currentContextPerThread.put(ccl, this.context);
|
||||||
|
}
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
|
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
|
||||||
|
|
@ -364,7 +375,13 @@ public class ContextLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
currentContextPerThread.remove(Thread.currentThread().getContextClassLoader());
|
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
|
||||||
|
if (ccl == ContextLoader.class.getClassLoader()) {
|
||||||
|
currentContext = null;
|
||||||
|
}
|
||||||
|
else if (ccl != null) {
|
||||||
|
currentContextPerThread.remove(ccl);
|
||||||
|
}
|
||||||
servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
|
servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
|
||||||
if (this.parentContextRef != null) {
|
if (this.parentContextRef != null) {
|
||||||
this.parentContextRef.release();
|
this.parentContextRef.release();
|
||||||
|
|
@ -382,7 +399,14 @@ public class ContextLoader {
|
||||||
* @see org.springframework.web.context.support.SpringBeanAutowiringSupport
|
* @see org.springframework.web.context.support.SpringBeanAutowiringSupport
|
||||||
*/
|
*/
|
||||||
public static WebApplicationContext getCurrentWebApplicationContext() {
|
public static WebApplicationContext getCurrentWebApplicationContext() {
|
||||||
return currentContextPerThread.get(Thread.currentThread().getContextClassLoader());
|
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
|
||||||
|
if (ccl != null) {
|
||||||
|
WebApplicationContext ccpt = currentContextPerThread.get(ccl);
|
||||||
|
if (ccpt != null) {
|
||||||
|
return ccpt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue