Skip to content

Commit

Permalink
Allow late binding ServletContextAwareProcessor
Browse files Browse the repository at this point in the history
Update ServletContextAwareProcessor to allow subclasses to support late
binding of the ServletConfig and/or ServletContext. Allows for the
post-processor to be registered before the servlet environment has been
initialized.

Issue: SPR-10381
  • Loading branch information
philwebb committed May 8, 2013
1 parent 19187fd commit e9a2e68
Showing 1 changed file with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -33,6 +33,7 @@
* 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
Expand All @@ -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.
*/
Expand All @@ -64,18 +73,36 @@ public ServletContextAwareProcessor(ServletConfig servletConfig) {
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;
}
Expand Down

0 comments on commit e9a2e68

Please sign in to comment.