《Spring MVC源码分析》

一、Spring MVC 框架快速体验

1.1 配置web.xml
1.2 创建Sring 配置文件的applicationContext.xml
1.3 创建model
1.4 创建controller

二、源码分析

2.1 ContextLoaderListener

ContextLoaderListener的作用是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法,使用servletContextListener接口。

2.1.1 Spring中的ContextLoaderListener

ServletContext启动之后会调用ServletContextListener的contextInitialized方法。

	public void contextInitialized(ServletContextEvent event) {
		// 初始化WebApplicationContext
		initWebApplicationContext(event.getServletContext());
	}
	public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
		try {
			if (this.context == null) {
				// 初始化context
				this.context = createWebApplicationContext(servletContext);
			}
			if (this.context instanceof ConfigurableWebApplicationContext) {
				ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
				if (!cwac.isActive()) {
					if (cwac.getParent() == null) {
					
						ApplicationContext parent = loadParentContext(servletContext);
						cwac.setParent(parent);
					}
					configureAndRefreshWebApplicationContext(cwac, servletContext);
				}
			}
			
			// 记录在servletContext中
			servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

			ClassLoader ccl = Thread.currentThread().getContextClassLoader();
			if (ccl == ContextLoader.class.getClassLoader()) {
				currentContext = this.context;
			}
			else if (ccl != null) {
				currentContextPerThread.put(ccl, this.context);
			}

			return this.context;
		}
		catch (RuntimeException | Error ex) {
			servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
			throw ex;
		}
	}

initWebApplciationContext函数主要是体现了创建WebApplicationConext实例的一个功能架构,从函数中我们看到了初始化的大致步骤
(1)WebApplicationContext存在性的校验
(2)创建WebApplicationContext实例

2.2 DispatcherServlet

在Spring中,ContextLoaderListener只是辅助功能,用于创建WebApplicationContext类型实例,而真正的逻辑实现其实是在DispatchServlet中进行的,DispatchServlet是实现servlet接口的实现类,主要分为以下几个阶段
(1)初始化阶段
(2)运行阶段
(3)销毁阶段

2.2.1 DispatcherServlet的初始化

父类初始化入口

	public final void init() throws ServletException {
		// 解析init-param并封装在pvs中
		PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
		if (!pvs.isEmpty()) {
			try {
				// 将当前的这个servlet类转换为一个beanWrapper,从而能够以Spring的方式来对init-param的值进行注入
				BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
				ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
				bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
				initBeanWrapper(bw);
				bw.setPropertyValues(pvs, true);
			}
			catch (BeansException ex) {
			}
		}

		// 子类扩展
		initServletBean();
	}

1.封装及校验初始化参数

	public ServletConfigPropertyValues(ServletConfig config, Set<String> requiredProperties)
				throws ServletException {

			Set<String> missingProps = (!CollectionUtils.isEmpty(requiredProperties) ?
					new HashSet<>(requiredProperties) : null);

			Enumeration<String> paramNames = config.getInitParameterNames();
			while (paramNames.hasMoreElements()) {
				String property = paramNames.nextElement();
				Object value = config.getInitParameter(property);
				addPropertyValue(new PropertyValue(property, value));
				if (missingProps != null) {
					missingProps.remove(property);
				}
			}

			// Fail if we are still missing properties.
			if (!CollectionUtils.isEmpty(missingProps)) {	
			}
		}
	}

2.将当前servlet实例化成beanWrapper实例
PropertyAccessorFactory.forBeanPropertyAccess是Spring中提供的工具方法,主要用于实例化为Spring中可以处理的BeanWrapper类型的实例

3.初始化ServletBean
init的实现委托给了initWebApplicationContext

2.2.2 WebApplicationConext的初始化

initWebApplicationConext方法的主要工作就是创建或刷新WebApplicationConext实例并对Servlet功能所使用的变量进行初始化。

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

		if (this.webApplicationContext != null) {
			// context实例在构造方法中被注入
			wac = this.webApplicationContext;
			if (wac instanceof ConfigurableWebApplicationContext) {
				ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
				if (!cwac.isActive()) {
				
					if (cwac.getParent() == null) {
						cwac.setParent(rootContext);
					}
					// 刷新上下文环境
					configureAndRefreshWebApplicationContext(cwac);
				}
			}
		}
		if (wac == null) {
			// 根据ContextAttribute属性加载WebApplicationContext
			wac = findWebApplicationContext();
		}
		if (wac == null) {
			// No context instance is defined for this servlet -> create a local one
			wac = createWebApplicationContext(rootContext);
		}

		if (!this.refreshEventReceived) {
			// Either the context is not a ConfigurableApplicationContext with refresh
			// support or the context injected at construction time had already been
			// refreshed -> trigger initial onRefresh manually here.
			synchronized (this.onRefreshMonitor) {
				onRefresh(wac);
			}
		}

		if (this.publishContext) {
			// Publish the context as a servlet context attribute.
			String attrName = getServletContextAttributeName();
			getServletContext().setAttribute(attrName, wac);
		}

		return wac;
	}

1.寻找或创建对应的WebApplicationConext实例
2. 对已经是闯进阿德WebApplicationContext实例进行配置及刷新
3. 刷新

2.2.3 DispatcherServlet的逻辑处理
2.2.4 MultipartContent类型的request处理
2.2.5 根据request信息寻找对应的Handler
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值