ServletContextAware接口的使用方法和注入ServletContext原理
在Spring中,凡是实现ServletContextAware接口的类,都可以取得ServletContext。实现如下:
private ServletContext application;
public void setServletContext(ServletContext servletContext) {
this.application = 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。
应用(使用servletcontext获取web根绝对路径):
public String get(String path) {
String realpath=context.getRealPath(path);
//tomcat8.0获取不到真实路径,通过/获取路径
if(realpath==null){
realpath=context.getRealPath(“/”)+path;
}
return realpath;
}
更多内容请长按二维码关注(更有不定期发红包活动吆^0^):

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《ServletContextAware接口的使用方法和注入ServletContext原理》
本文地址:http://www.xiupu.net/archives-7213.html
关注公众号:
微信赞赏
支付宝赞赏
