1 16 17 package org.springframework.web.context.support; 18 19 import javax.servlet.ServletConfig ; 20 import javax.servlet.ServletContext ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import org.springframework.beans.BeansException; 26 import org.springframework.beans.factory.config.BeanPostProcessor; 27 import org.springframework.web.context.ServletConfigAware; 28 import org.springframework.web.context.ServletContextAware; 29 30 42 public class ServletContextAwareProcessor implements BeanPostProcessor { 43 44 protected final Log logger = LogFactory.getLog(getClass()); 45 46 private ServletContext servletContext; 47 48 private ServletConfig servletConfig; 49 50 51 54 public ServletContextAwareProcessor(ServletContext servletContext) { 55 this(servletContext, null); 56 } 57 58 61 public ServletContextAwareProcessor(ServletConfig servletConfig) { 62 this(null, servletConfig); 63 } 64 65 68 public ServletContextAwareProcessor(ServletContext servletContext, ServletConfig servletConfig) { 69 this.servletContext = servletContext; 70 this.servletConfig = servletConfig; 71 if (servletContext == null && servletConfig != null) { 72 this.servletContext = servletConfig.getServletContext(); 73 } 74 } 75 76 77 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 78 if (bean instanceof ServletContextAware) { 79 if (this.servletContext == null) { 80 throw new IllegalStateException ("Cannot satisfy ServletContextAware for bean '" + 81 beanName + "' without ServletContext"); 82 } 83 if (logger.isDebugEnabled()) { 84 logger.debug("Invoking setServletContext on ServletContextAware bean '" + beanName + "'"); 85 } 86 ((ServletContextAware) bean).setServletContext(this.servletContext); 87 } 88 if (bean instanceof ServletConfigAware) { 89 if (this.servletConfig == null) { 90 throw new IllegalStateException ("Cannot satisfy ServletConfigAware for bean '" + 91 beanName + "' without ServletConfig"); 92 } 93 if (logger.isDebugEnabled()) { 94 logger.debug("Invoking setServletConfig on ServletConfigAware bean '" + beanName + "'"); 95 } 96 ((ServletConfigAware) bean).setServletConfig(this.servletConfig); 97 } 98 return bean; 99 } 100 101 public Object postProcessAfterInitialization(Object bean, String beanName) { 102 return bean; 103 } 104 105 } 106 | Popular Tags |