Spring 的WebApplicationContext是如何被实例化的

ContextLoaderListener是ServletContextListener的一种实现,这个接口的作用就是监听ServletContext的生命周期内发生的事件,包括其初始化和销毁

public interface ServletContextListener extends EventListener {
    /**
     * Receives notification that the web application initialization
     * process is starting.
     */
    public void contextInitialized(ServletContextEvent sce);

    /**
     * Receives notification that the ServletContext is about to be
     * shut down.
     */
    public void contextDestroyed(ServletContextEvent sce);
}

在web.xml声明的ServeltContextListener必须拥有一个无参的构造方法;对象创建好之后就可以监听ServletContext的生命周期了;

当ServletContext初始化时,调用其初始化方法,对WebApplicationContext进行初始化。

	@Override
	public void contextInitialized(ServletContextEvent event) {
		initWebApplicationContext(event.getServletContext());
	}

创建好的ApplicationContext实例注册到ServletContext的属性中,其对应的属性名为WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;并且当contextDestroyed被调用时这个Spring的ApplicationContext将会被关闭。

String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
			if (this.context == null) {
				this.context = createWebApplicationContext(servletContext);
			}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

其ApplicationContext是一个 org.springframework.web.context.support.XmlWebApplicationContext的实例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值