SpringMVC创建过程(二):FrameworkServlet

在这里插入图片描述

  1. 从HttpServletBean中知,FrameworkServlet的初始化入口方法是initServletBean(),其代码如下:
    /**
	 * Overridden method of {@link HttpServletBean}, invoked after any bean properties
	 * have been set. Creates this servlet's WebApplicationContext.
	 */
	@Override
	protected final void initServletBean() throws ServletException {
		getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
		if (this.logger.isInfoEnabled()) {
			this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
		}
		long startTime = System.currentTimeMillis();

		try {
		  //初始化webApplicationContext
			this.webApplicationContext = initWebApplicationContext();
			//初始化FrameworkServlet,子类可以覆盖然后在里面做一些初始化工作,但子类并没有使用它
			initFrameworkServlet();
		}
		catch (ServletException | RuntimeException ex) {
			this.logger.error("Context initialization failed", ex);
			throw ex;
		}

		if (this.logger.isInfoEnabled()) {
			long elapsedTime = System.currentTimeMillis() - startTime;
			this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
					elapsedTime + " ms");
		}
	}

1.1 可见FrameworkServlet在构建过程中的主要作用就是初始化webApplicationContext

	protected WebApplicationContext initWebApplicationContext() {
		//获取rootContext
		WebApplicationContext rootContext =
				WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		WebApplicationContext wac = null;

		//如果已经通过构造方法设置了webApplicationContext(第一种获取WebApplicationContext的方法)
		if (this.webApplicationContext != null) {
			// A context instance was injected at construction time -> use it
			wac = this.webApplicationContext;
			if (wac instanceof ConfigurableWebApplicationContext) {
				ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
				if (!cwac.isActive()) {
					// The context has not yet been refreshed -> provide services such as
					// setting the parent context, setting the application context id, etc
					if (cwac.getParent() == null) {
						// The context instance was injected without an explicit parent -> set
						// the root application context (if any; may be null) as the parent
						cwac.setParent(rootContext);
					}
					configureAndRefreshWebApplicationContext(cwac);
				}
			}
		}
		if (wac == null) {
			/**
    		* 通过配置在ServletContext中的参数contextAttribute中获取WebApplicationContext
     		* (同时在本servlet被初始化或调用之前,WebApplicationContext必须已经在ServletContext中被加载或者存储)
     		* (第二种获取WebApplicationContext的方法)
      		*/
			wac = findWebApplicationContext();
		}
		if (wac == null) {
			//还没有webApplicationContext,创建一个.正常情况下用这个
			//(第三种获取WebApplicationContext的方法)
			wac = createWebApplicationContext(rootContext);
		}

		if (!this.refreshEventReceived) {
			//当ContextRefreshedEvent事件没有被触发时调用
			onRefresh(wac);
		}

		if (this.publishContext) {
			//将WebApplicationContext保存到ServletContext中
			String attrName = getServletContextAttributeName();
			getServletContext().setAttribute(attrName, wac);
			if (this.logger.isDebugEnabled()) {
				this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
						"' as ServletContext attribute with name [" + attrName + "]");
			}
		}

		return wac;
	}

总结: initWebApplicationContext方法做了三件事
a) 获取spring的根容器rootContext
b) 设置WebApplicationContext并根据情况调用onRefresh方法,不管用哪种方式调用onRefresh最终肯定会被调用一次,而且只调用一次
c) 将webApplicationContext设置到ServletContext中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值