在Spring中,凡是實現ServletContextAware接口的類,都可以取得ServletContext。實現如下:
// 獲取上下文
private ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
那麼Spring是在什麼時候把ServletContext放置進去的呢?通過對Spring的學習,終於明白了。
在web項目中,Spring容器的加載是通過XmlWebApplicationContext進行的。
它的父類AbstractRefreshableWebApplicationContext,在postProcessBeanFactory方法中進行了如下操作(postProcessBeanFactory方法被AbstractApplicationContext的refresh方法調用)
beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig));
beanFactory.ignoreDependencyInterface(ServletContextAware.class);
beanFactory.ignoreDependencyInterface(ServletConfigAware.class);
WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext);
WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.servletContext, this.servletConfig);
代碼的第一句就是添加了一個ServletContextAwareProcessor。
該類的postProcessBeforeInitialization方法如下:
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (this.servletContext != null && bean instanceof ServletContextAware) {
((ServletContextAware) bean).setServletContext(this.servletContext);
}
if (this.servletConfig != null && bean instanceof ServletConfigAware) {
((ServletConfigAware) bean).setServletConfig(this.servletConfig);
}
return bean;
}
而所有的BeanPostProcessor都將在AbstractAutowireCapableBeanFactory類的initializeBean方法中,通過調用applyBeanPostProcessorsBeforeInitialization方法完成所有實現BeanPostProcessor接口的postProcessBeforeInitialization的調用。
XmlWebApplicationContext使用的BeanFactory是DefaultListableBeanFactory。
DefaultListableBeanFactory繼承了AbstractAutowireCapableBeanFactory,因此可以完成上述操作。
如此完成了只要實現了ServletContextAware接口的,都可以獲取ServletContext。