diff --git a/spring-web/src/main/java/org/springframework/web/context/support/ServletContextAwareProcessor.java b/spring-web/src/main/java/org/springframework/web/context/support/ServletContextAwareProcessor.java index 8970e16c8b..8c3c688a3c 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/ServletContextAwareProcessor.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/ServletContextAwareProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ import org.springframework.web.context.ServletContextAware; * underlying bean factory. Applications do not use this directly. * * @author Juergen Hoeller + * @author Phillip Webb * @since 12.03.2004 * @see org.springframework.web.context.ServletContextAware * @see org.springframework.web.context.support.XmlWebApplicationContext#postProcessBeanFactory @@ -44,6 +45,14 @@ public class ServletContextAwareProcessor implements BeanPostProcessor { private ServletConfig servletConfig; + /** + * Create a new ServletContextAwareProcessor without an initial context or config. + * When this constructor is used the {@link #getServletContext()} and/or + * {@link #getServletConfig()} methods should be overriden. + */ + protected ServletContextAwareProcessor() { + } + /** * Create a new ServletContextAwareProcessor for the given context. */ @@ -64,18 +73,36 @@ public class ServletContextAwareProcessor implements BeanPostProcessor { public ServletContextAwareProcessor(ServletContext servletContext, ServletConfig servletConfig) { this.servletContext = servletContext; this.servletConfig = servletConfig; - if (servletContext == null && servletConfig != null) { - this.servletContext = servletConfig.getServletContext(); + } + + + /** + * Returns the {@link ServletContext} to be injected or {@code null}. This method + * can be overridden by subclasses when a context is obtained after the post-processor + * has been registered. + */ + protected ServletContext getServletContext() { + if(this.servletContext == null && getServletConfig() != null) { + return getServletConfig().getServletContext(); } + return this.servletContext; } + /** + * Returns the {@link ServletContext} to be injected or {@code null}. This method + * can be overridden by subclasses when a context is obtained after the post-processor + * has been registered. + */ + protected ServletConfig getServletConfig() { + return this.servletConfig; + } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - if (this.servletContext != null && bean instanceof ServletContextAware) { - ((ServletContextAware) bean).setServletContext(this.servletContext); + if (getServletContext() != null && bean instanceof ServletContextAware) { + ((ServletContextAware) bean).setServletContext(getServletContext()); } - if (this.servletConfig != null && bean instanceof ServletConfigAware) { - ((ServletConfigAware) bean).setServletConfig(this.servletConfig); + if (getServletConfig() != null && bean instanceof ServletConfigAware) { + ((ServletConfigAware) bean).setServletConfig(getServletConfig()); } return bean; }