revised version checks and exception signatures

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2160 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2009-10-20 16:53:24 +00:00
parent 1e7a0401d0
commit af32913f07
5 changed files with 57 additions and 91 deletions

View File

@ -42,7 +42,6 @@ import javax.portlet.UnavailableException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@ -583,10 +582,9 @@ public class DispatcherPortlet extends FrameworkPortlet {
* @param context the current Portlet ApplicationContext * @param context the current Portlet ApplicationContext
* @param clazz the strategy implementation class to instantiate * @param clazz the strategy implementation class to instantiate
* @return the fully configured strategy instance * @return the fully configured strategy instance
* @throws BeansException if initialization failed
* @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory() * @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()
*/ */
protected Object createDefaultStrategy(ApplicationContext context, Class<?> clazz) throws BeansException { protected Object createDefaultStrategy(ApplicationContext context, Class<?> clazz) {
return context.getAutowireCapableBeanFactory().createBean(clazz); return context.getAutowireCapableBeanFactory().createBean(clazz);
} }

View File

@ -32,7 +32,6 @@ import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse; import javax.portlet.ResourceResponse;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationContextException;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
@ -258,7 +257,7 @@ public abstract class FrameworkPortlet extends GenericPortletBean
* have been set. Creates this portlet's ApplicationContext. * have been set. Creates this portlet's ApplicationContext.
*/ */
@Override @Override
protected final void initPortletBean() throws PortletException, BeansException { protected final void initPortletBean() throws PortletException {
getPortletContext().log("Initializing Spring FrameworkPortlet '" + getPortletName() + "'"); getPortletContext().log("Initializing Spring FrameworkPortlet '" + getPortletName() + "'");
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("FrameworkPortlet '" + getPortletName() + "': initialization started"); logger.info("FrameworkPortlet '" + getPortletName() + "': initialization started");
@ -273,7 +272,7 @@ public abstract class FrameworkPortlet extends GenericPortletBean
logger.error("Context initialization failed", ex); logger.error("Context initialization failed", ex);
throw ex; throw ex;
} }
catch (BeansException ex) { catch (RuntimeException ex) {
logger.error("Context initialization failed", ex); logger.error("Context initialization failed", ex);
throw ex; throw ex;
} }
@ -289,9 +288,8 @@ public abstract class FrameworkPortlet extends GenericPortletBean
* <p>Delegates to {@link #createPortletApplicationContext} for actual creation. * <p>Delegates to {@link #createPortletApplicationContext} for actual creation.
* Can be overridden in subclasses. * Can be overridden in subclasses.
* @return the ApplicationContext for this portlet * @return the ApplicationContext for this portlet
* @throws BeansException if the context couldn't be initialized
*/ */
protected ApplicationContext initPortletApplicationContext() throws BeansException { protected ApplicationContext initPortletApplicationContext() {
ApplicationContext parent = PortletApplicationContextUtils.getWebApplicationContext(getPortletContext()); ApplicationContext parent = PortletApplicationContextUtils.getWebApplicationContext(getPortletContext());
ApplicationContext pac = createPortletApplicationContext(parent); ApplicationContext pac = createPortletApplicationContext(parent);
@ -320,13 +318,10 @@ public abstract class FrameworkPortlet extends GenericPortletBean
* ConfigurablePortletApplicationContext. Can be overridden in subclasses. * ConfigurablePortletApplicationContext. Can be overridden in subclasses.
* @param parent the parent ApplicationContext to use, or null if none * @param parent the parent ApplicationContext to use, or null if none
* @return the Portlet ApplicationContext for this portlet * @return the Portlet ApplicationContext for this portlet
* @throws BeansException if the context couldn't be initialized
* @see #setContextClass * @see #setContextClass
* @see org.springframework.web.portlet.context.XmlPortletApplicationContext * @see org.springframework.web.portlet.context.XmlPortletApplicationContext
*/ */
protected ApplicationContext createPortletApplicationContext(ApplicationContext parent) protected ApplicationContext createPortletApplicationContext(ApplicationContext parent) {
throws BeansException {
Class<?> contextClass = getContextClass(); Class<?> contextClass = getContextClass();
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Portlet with name '" + getPortletName() + logger.debug("Portlet with name '" + getPortletName() +
@ -338,7 +333,6 @@ public abstract class FrameworkPortlet extends GenericPortletBean
"': custom ApplicationContext class [" + contextClass.getName() + "': custom ApplicationContext class [" + contextClass.getName() +
"] is not of type ConfigurablePortletApplicationContext"); "] is not of type ConfigurablePortletApplicationContext");
} }
ConfigurablePortletApplicationContext pac = ConfigurablePortletApplicationContext pac =
(ConfigurablePortletApplicationContext) BeanUtils.instantiateClass(contextClass); (ConfigurablePortletApplicationContext) BeanUtils.instantiateClass(contextClass);
@ -400,19 +394,17 @@ public abstract class FrameworkPortlet extends GenericPortletBean
* <p>The default implementation is empty; subclasses may override this method * <p>The default implementation is empty; subclasses may override this method
* to perform any initialization they require. * to perform any initialization they require.
* @throws PortletException in case of an initialization exception * @throws PortletException in case of an initialization exception
* @throws BeansException if thrown by ApplicationContext methods
*/ */
protected void initFrameworkPortlet() throws PortletException, BeansException { protected void initFrameworkPortlet() throws PortletException {
} }
/** /**
* Refresh this portlet's application context, as well as the * Refresh this portlet's application context, as well as the
* dependent state of the portlet. * dependent state of the portlet.
* @throws BeansException in case of errors
* @see #getPortletApplicationContext() * @see #getPortletApplicationContext()
* @see org.springframework.context.ConfigurableApplicationContext#refresh() * @see org.springframework.context.ConfigurableApplicationContext#refresh()
*/ */
public void refresh() throws BeansException { public void refresh() {
ApplicationContext pac = getPortletApplicationContext(); ApplicationContext pac = getPortletApplicationContext();
if (!(pac instanceof ConfigurableApplicationContext)) { if (!(pac instanceof ConfigurableApplicationContext)) {
throw new IllegalStateException("Portlet ApplicationContext does not support refresh: " + pac); throw new IllegalStateException("Portlet ApplicationContext does not support refresh: " + pac);
@ -438,10 +430,9 @@ public abstract class FrameworkPortlet extends GenericPortletBean
* Called after successful context refresh. * Called after successful context refresh.
* <p>This implementation is empty. * <p>This implementation is empty.
* @param context the current Portlet ApplicationContext * @param context the current Portlet ApplicationContext
* @throws BeansException in case of errors
* @see #refresh() * @see #refresh()
*/ */
protected void onRefresh(ApplicationContext context) throws BeansException { protected void onRefresh(ApplicationContext context) {
// For subclasses: do nothing by default. // For subclasses: do nothing by default.
} }

View File

@ -35,7 +35,6 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@ -322,7 +321,7 @@ public class DispatcherServlet extends FrameworkServlet {
* This implementation calls {@link #initStrategies}. * This implementation calls {@link #initStrategies}.
*/ */
@Override @Override
protected void onRefresh(ApplicationContext context) throws BeansException { protected void onRefresh(ApplicationContext context) {
initStrategies(context); initStrategies(context);
} }
@ -673,11 +672,10 @@ public class DispatcherServlet extends FrameworkServlet {
* @param context the current WebApplicationContext * @param context the current WebApplicationContext
* @param clazz the strategy implementation class to instantiate * @param clazz the strategy implementation class to instantiate
* @return the fully configured strategy instance * @return the fully configured strategy instance
* @throws BeansException if initialization failed
* @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory() * @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()
* @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean
*/ */
protected Object createDefaultStrategy(ApplicationContext context, Class clazz) throws BeansException { protected Object createDefaultStrategy(ApplicationContext context, Class<?> clazz) {
return context.getAutowireCapableBeanFactory().createBean(clazz); return context.getAutowireCapableBeanFactory().createBean(clazz);
} }
@ -742,7 +740,7 @@ public class DispatcherServlet extends FrameworkServlet {
int interceptorIndex = -1; int interceptorIndex = -1;
try { try {
ModelAndView mv = null; ModelAndView mv;
boolean errorView = false; boolean errorView = false;
try { try {
@ -1032,13 +1030,11 @@ public class DispatcherServlet extends FrameworkServlet {
* @throws Exception if there's a problem rendering the view * @throws Exception if there's a problem rendering the view
*/ */
protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception { protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Determine locale for request and apply it to the response. // Determine locale for request and apply it to the response.
Locale locale = this.localeResolver.resolveLocale(request); Locale locale = this.localeResolver.resolveLocale(request);
response.setLocale(locale); response.setLocale(locale);
View view = null; View view;
if (mv.isReference()) { if (mv.isReference()) {
// We need to resolve the view name. // We need to resolve the view name.
view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request); view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
@ -1076,7 +1072,7 @@ public class DispatcherServlet extends FrameworkServlet {
/** /**
* Resolve the given view name into a View object (to be rendered). * Resolve the given view name into a View object (to be rendered).
* <p>Default implementations asks all ViewResolvers of this dispatcher. * <p>The default implementations asks all ViewResolvers of this dispatcher.
* Can be overridden for custom resolution strategies, potentially based on * Can be overridden for custom resolution strategies, potentially based on
* specific model attributes or request parameters. * specific model attributes or request parameters.
* @param viewName the name of the view to resolve * @param viewName the name of the view to resolve
@ -1088,9 +1084,7 @@ public class DispatcherServlet extends FrameworkServlet {
* (typically in case of problems creating an actual View object) * (typically in case of problems creating an actual View object)
* @see ViewResolver#resolveViewName * @see ViewResolver#resolveViewName
*/ */
protected View resolveViewName(String viewName, protected View resolveViewName(String viewName, Map<String, Object> model, Locale locale,
Map<String, Object> model,
Locale locale,
HttpServletRequest request) throws Exception { HttpServletRequest request) throws Exception {
for (ViewResolver viewResolver : this.viewResolvers) { for (ViewResolver viewResolver : this.viewResolvers) {

View File

@ -24,7 +24,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationContextException;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
@ -297,7 +296,7 @@ public abstract class FrameworkServlet extends HttpServletBean
* have been set. Creates this servlet's WebApplicationContext. * have been set. Creates this servlet's WebApplicationContext.
*/ */
@Override @Override
protected final void initServletBean() throws ServletException, BeansException { protected final void initServletBean() throws ServletException {
getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'"); getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
if (this.logger.isInfoEnabled()) { if (this.logger.isInfoEnabled()) {
this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started"); this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
@ -312,7 +311,7 @@ public abstract class FrameworkServlet extends HttpServletBean
this.logger.error("Context initialization failed", ex); this.logger.error("Context initialization failed", ex);
throw ex; throw ex;
} }
catch (BeansException ex) { catch (RuntimeException ex) {
this.logger.error("Context initialization failed", ex); this.logger.error("Context initialization failed", ex);
throw ex; throw ex;
} }
@ -329,11 +328,10 @@ public abstract class FrameworkServlet extends HttpServletBean
* <p>Delegates to {@link #createWebApplicationContext} for actual creation * <p>Delegates to {@link #createWebApplicationContext} for actual creation
* of the context. Can be overridden in subclasses. * of the context. Can be overridden in subclasses.
* @return the WebApplicationContext instance * @return the WebApplicationContext instance
* @throws BeansException if the context couldn't be initialized
* @see #setContextClass * @see #setContextClass
* @see #setContextConfigLocation * @see #setContextConfigLocation
*/ */
protected WebApplicationContext initWebApplicationContext() throws BeansException { protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext wac = findWebApplicationContext(); WebApplicationContext wac = findWebApplicationContext();
if (wac == null) { if (wac == null) {
// No fixed context defined for this servlet - create a local one. // No fixed context defined for this servlet - create a local one.
@ -397,12 +395,9 @@ public abstract class FrameworkServlet extends HttpServletBean
* before returning the context instance. * before returning the context instance.
* @param parent the parent ApplicationContext to use, or <code>null</code> if none * @param parent the parent ApplicationContext to use, or <code>null</code> if none
* @return the WebApplicationContext for this servlet * @return the WebApplicationContext for this servlet
* @throws BeansException if the context couldn't be initialized
* @see org.springframework.web.context.support.XmlWebApplicationContext * @see org.springframework.web.context.support.XmlWebApplicationContext
*/ */
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
throws BeansException {
Class<?> contextClass = getContextClass(); Class<?> contextClass = getContextClass();
if (this.logger.isDebugEnabled()) { if (this.logger.isDebugEnabled()) {
this.logger.debug("Servlet with name '" + getServletName() + this.logger.debug("Servlet with name '" + getServletName() +
@ -415,26 +410,27 @@ public abstract class FrameworkServlet extends HttpServletBean
"': custom WebApplicationContext class [" + contextClass.getName() + "': custom WebApplicationContext class [" + contextClass.getName() +
"] is not of type ConfigurableWebApplicationContext"); "] is not of type ConfigurableWebApplicationContext");
} }
ConfigurableWebApplicationContext wac = ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
// Assign the best possible id value. // Assign the best possible id value.
ServletContext servletContext = getServletContext(); ServletContext sc = getServletContext();
if (servletContext.getMajorVersion() > 2 || servletContext.getMinorVersion() >= 5) { if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
// Servlet 2.5's getContextPath available!
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContext.getContextPath() + "/" + getServletName());
}
else {
// Servlet <= 2.4: resort to name specified in web.xml, if any. // Servlet <= 2.4: resort to name specified in web.xml, if any.
String servletContextName = servletContext.getServletContextName(); String servletContextName = sc.getServletContextName();
if (servletContextName != null) { if (servletContextName != null) {
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName + "." + getServletName()); wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName +
"." + getServletName());
} }
else { else {
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + getServletName()); wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + getServletName());
} }
} }
else {
// Servlet 2.5's getContextPath available!
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + sc.getContextPath() +
"/" + getServletName());
}
wac.setParent(parent); wac.setParent(parent);
wac.setServletContext(getServletContext()); wac.setServletContext(getServletContext());
@ -485,19 +481,17 @@ public abstract class FrameworkServlet extends HttpServletBean
* the WebApplicationContext has been loaded. The default implementation is empty; * the WebApplicationContext has been loaded. The default implementation is empty;
* subclasses may override this method to perform any initialization they require. * subclasses may override this method to perform any initialization they require.
* @throws ServletException in case of an initialization exception * @throws ServletException in case of an initialization exception
* @throws BeansException if thrown by ApplicationContext methods
*/ */
protected void initFrameworkServlet() throws ServletException, BeansException { protected void initFrameworkServlet() throws ServletException {
} }
/** /**
* Refresh this servlet's application context, as well as the * Refresh this servlet's application context, as well as the
* dependent state of the servlet. * dependent state of the servlet.
* @throws BeansException in case of errors
* @see #getWebApplicationContext() * @see #getWebApplicationContext()
* @see org.springframework.context.ConfigurableApplicationContext#refresh() * @see org.springframework.context.ConfigurableApplicationContext#refresh()
*/ */
public void refresh() throws BeansException { public void refresh() {
WebApplicationContext wac = getWebApplicationContext(); WebApplicationContext wac = getWebApplicationContext();
if (!(wac instanceof ConfigurableApplicationContext)) { if (!(wac instanceof ConfigurableApplicationContext)) {
throw new IllegalStateException("WebApplicationContext does not support refresh: " + wac); throw new IllegalStateException("WebApplicationContext does not support refresh: " + wac);
@ -523,10 +517,9 @@ public abstract class FrameworkServlet extends HttpServletBean
* Called after successful context refresh. * Called after successful context refresh.
* <p>This implementation is empty. * <p>This implementation is empty.
* @param context the current WebApplicationContext * @param context the current WebApplicationContext
* @throws BeansException in case of errors
* @see #refresh() * @see #refresh()
*/ */
protected void onRefresh(ApplicationContext context) throws BeansException { protected void onRefresh(ApplicationContext context) {
// For subclasses: do nothing by default. // For subclasses: do nothing by default.
} }
@ -540,7 +533,7 @@ public abstract class FrameworkServlet extends HttpServletBean
*/ */
@Override @Override
protected final void doGet(HttpServletRequest request, HttpServletResponse response) protected final void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
processRequest(request, response); processRequest(request, response);
} }
@ -551,7 +544,7 @@ public abstract class FrameworkServlet extends HttpServletBean
*/ */
@Override @Override
protected final void doPost(HttpServletRequest request, HttpServletResponse response) protected final void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
processRequest(request, response); processRequest(request, response);
} }
@ -562,7 +555,7 @@ public abstract class FrameworkServlet extends HttpServletBean
*/ */
@Override @Override
protected final void doPut(HttpServletRequest request, HttpServletResponse response) protected final void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
processRequest(request, response); processRequest(request, response);
} }
@ -573,7 +566,7 @@ public abstract class FrameworkServlet extends HttpServletBean
*/ */
@Override @Override
protected final void doDelete(HttpServletRequest request, HttpServletResponse response) protected final void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
processRequest(request, response); processRequest(request, response);
} }
@ -584,7 +577,9 @@ public abstract class FrameworkServlet extends HttpServletBean
* @see #doService * @see #doService
*/ */
@Override @Override
protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doOptions(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
super.doOptions(request, response); super.doOptions(request, response);
if (this.dispatchOptionsRequest) { if (this.dispatchOptionsRequest) {
processRequest(request, response); processRequest(request, response);
@ -597,7 +592,9 @@ public abstract class FrameworkServlet extends HttpServletBean
* @see #doService * @see #doService
*/ */
@Override @Override
protected void doTrace(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doTrace(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
super.doTrace(request, response); super.doTrace(request, response);
if (this.dispatchTraceRequest) { if (this.dispatchTraceRequest) {
processRequest(request, response); processRequest(request, response);
@ -715,7 +712,7 @@ public abstract class FrameworkServlet extends HttpServletBean
* @see javax.servlet.http.HttpServlet#doPost * @see javax.servlet.http.HttpServlet#doPost
*/ */
protected abstract void doService(HttpServletRequest request, HttpServletResponse response) protected abstract void doService(HttpServletRequest request, HttpServletResponse response)
throws Exception; throws Exception;
/** /**

View File

@ -26,7 +26,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.access.BeanFactoryLocator; import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference; import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -167,14 +166,10 @@ public class ContextLoader {
* "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params. * "{@link #CONFIG_LOCATION_PARAM contextConfigLocation}" context-params.
* @param servletContext current servlet context * @param servletContext current servlet context
* @return the new WebApplicationContext * @return the new WebApplicationContext
* @throws IllegalStateException if there is already a root application context present
* @throws BeansException if the context failed to initialize
* @see #CONTEXT_CLASS_PARAM * @see #CONTEXT_CLASS_PARAM
* @see #CONFIG_LOCATION_PARAM * @see #CONFIG_LOCATION_PARAM
*/ */
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
throws IllegalStateException, BeansException {
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) { if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException( throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " + "Cannot initialize context because there is already a root application context present - " +
@ -229,32 +224,24 @@ public class ContextLoader {
* Can be overridden in subclasses. * Can be overridden in subclasses.
* <p>In addition, {@link #customizeContext} gets called prior to refreshing the * <p>In addition, {@link #customizeContext} gets called prior to refreshing the
* context, allowing subclasses to perform custom modifications to the context. * context, allowing subclasses to perform custom modifications to the context.
* @param servletContext current servlet context * @param sc current servlet context
* @param parent the parent ApplicationContext to use, or <code>null</code> if none * @param parent the parent ApplicationContext to use, or <code>null</code> if none
* @return the root WebApplicationContext * @return the root WebApplicationContext
* @throws BeansException if the context couldn't be initialized
* @see ConfigurableWebApplicationContext * @see ConfigurableWebApplicationContext
*/ */
protected WebApplicationContext createWebApplicationContext( protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent) {
ServletContext servletContext, ApplicationContext parent) throws BeansException { Class<?> contextClass = determineContextClass(sc);
Class<?> contextClass = determineContextClass(servletContext);
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() + throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]"); "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
} }
ConfigurableWebApplicationContext wac = ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
// Assign the best possible id value. // Assign the best possible id value.
if (servletContext.getMajorVersion() > 2 || servletContext.getMinorVersion() >= 5) { if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
// Servlet 2.5's getContextPath available!
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContext.getContextPath());
}
else {
// Servlet <= 2.4: resort to name specified in web.xml, if any. // Servlet <= 2.4: resort to name specified in web.xml, if any.
String servletContextName = servletContext.getServletContextName(); String servletContextName = sc.getServletContextName();
if (servletContextName != null) { if (servletContextName != null) {
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName); wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName);
} }
@ -262,13 +249,16 @@ public class ContextLoader {
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX); wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX);
} }
} }
else {
// Servlet 2.5's getContextPath available!
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + sc.getContextPath());
}
wac.setParent(parent); wac.setParent(parent);
wac.setServletContext(servletContext); wac.setServletContext(sc);
wac.setConfigLocation(servletContext.getInitParameter(CONFIG_LOCATION_PARAM)); wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));
customizeContext(servletContext, wac); customizeContext(sc, wac);
wac.refresh(); wac.refresh();
return wac; return wac;
} }
@ -277,11 +267,10 @@ public class ContextLoader {
* default XmlWebApplicationContext or a custom context class if specified. * default XmlWebApplicationContext or a custom context class if specified.
* @param servletContext current servlet context * @param servletContext current servlet context
* @return the WebApplicationContext implementation class to use * @return the WebApplicationContext implementation class to use
* @throws ApplicationContextException if the context class couldn't be loaded
* @see #CONTEXT_CLASS_PARAM * @see #CONTEXT_CLASS_PARAM
* @see org.springframework.web.context.support.XmlWebApplicationContext * @see org.springframework.web.context.support.XmlWebApplicationContext
*/ */
protected Class determineContextClass(ServletContext servletContext) throws ApplicationContextException { protected Class<?> determineContextClass(ServletContext servletContext) {
String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM); String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
if (contextClassName != null) { if (contextClassName != null) {
try { try {
@ -336,12 +325,9 @@ public class ContextLoader {
* which also use the same configuration parameters. * which also use the same configuration parameters.
* @param servletContext current servlet context * @param servletContext current servlet context
* @return the parent application context, or <code>null</code> if none * @return the parent application context, or <code>null</code> if none
* @throws BeansException if the context couldn't be initialized
* @see org.springframework.context.access.ContextSingletonBeanFactoryLocator * @see org.springframework.context.access.ContextSingletonBeanFactoryLocator
*/ */
protected ApplicationContext loadParentContext(ServletContext servletContext) protected ApplicationContext loadParentContext(ServletContext servletContext) {
throws BeansException {
ApplicationContext parentContext = null; ApplicationContext parentContext = null;
String locatorFactorySelector = servletContext.getInitParameter(LOCATOR_FACTORY_SELECTOR_PARAM); String locatorFactorySelector = servletContext.getInitParameter(LOCATOR_FACTORY_SELECTOR_PARAM);
String parentContextKey = servletContext.getInitParameter(LOCATOR_FACTORY_KEY_PARAM); String parentContextKey = servletContext.getInitParameter(LOCATOR_FACTORY_KEY_PARAM);