上一篇笔记(Spring MVC源码——Root WebApplicationContext)中记录了下 Root WebApplicationContext 的初始化代码.这一篇来看 Servlet WebApplicationContext 的初始化代码

DispatcherServlet 是另一个需要在 web.xml 中配置的类, Servlet WebApplicationContext 就由它来创建和初始化.

HttpServletBean

HttpServletBean 简单继承了 HttpServlet, 负责将 init-param 中的参数注入到当前 Servlet 实例的属性中, 并且为子类提供了增加 requiredProperties 的能力. HttpServletBean 并不依赖于 Spring 容器.

来看一下它的 init() 方法:

  1. public final void init() throws ServletException {
  2. // Set bean properties from init parameters.
  3. // 从 ServletConfig 中取出初始化参数到 PropertyValues。ServletConfigPropertyValues 的构造器中将会检查是否缺失了必要属性
  4. PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
  5. if (!pvs.isEmpty()) {
  6. try {
  7. // 将 servlet 对象包装成 BeanWrapper ,从而能够以 Spring 的方式(反射)来注入参数
  8. BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
  9. ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
  10. // 注册 PropertyEditor,遇到 Resource 类型的属性时,用 ResourceEditor 解析
  11. bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
  12. // 初始化 BeanWrapper,空方法
  13. initBeanWrapper(bw);
  14. // 注入属性,忽略没有 setter 的属性
  15. bw.setPropertyValues(pvs, true);
  16. }
  17. catch (BeansException ex) {
  18. if (logger.isErrorEnabled()) {
  19. logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
  20. }
  21. throw ex;
  22. }
  23. }
  24. // Let subclasses do whatever initialization they like.
  25. // 由子类实现初始化逻辑
  26. initServletBean();
  27. }
  1. private static class ServletConfigPropertyValues extends MutablePropertyValues {
  2. public ServletConfigPropertyValues(ServletConfig config, Set<String> requiredProperties)
  3. throws ServletException {
  4. // 将 requiredProperties 拷贝到新的 Set missingProps
  5. Set<String> missingProps = (!CollectionUtils.isEmpty(requiredProperties) ?
  6. new HashSet<>(requiredProperties) : null);
  7.  
  8. // 将 ServletConfig 中的初始化参数取出,添加到 MutablePropertyValues 中
  9. Enumeration<String> paramNames = config.getInitParameterNames();
  10. while (paramNames.hasMoreElements()) {
  11. String property = paramNames.nextElement();
  12. Object value = config.getInitParameter(property);
  13. addPropertyValue(new PropertyValue(property, value));
  14. if (missingProps != null) {
  15. missingProps.remove(property);
  16. }
  17. }
  18.  
  19. // Fail if we are still missing properties.
  20. if (!CollectionUtils.isEmpty(missingProps)) {
  21. // 存在必须出现的条件没出现
  22. throw new ServletException(
  23. "Initialization from ServletConfig for servlet '" + config.getServletName() +
  24. "' failed; the following required properties were missing: " +
  25. StringUtils.collectionToDelimitedString(missingProps, ", "));
  26. }
  27. }
  28. }

FrameworkServlet

FrameworkServlet 是一个更具体的 Servlet 基类. 它有以下两个功能:

  • 每个 servket 管理一个 WebApplicationContext 实例.
  • 无论请求是否成功, 根据请求处理发布事件.

FrameworkServlet 重写了 HttpServletBean 的 initServletBean() 方法, 这个方法会在 所有 servlet 的属性被注入之后执行, 来看一下代码:

  1. protected final void initServletBean() throws ServletException {
  2. getServletContext().log("Initializing Spring " + getClass().getSimpleName() + " '" + getServletName() + "'");
  3. if (logger.isInfoEnabled()) {
  4. logger.info("Initializing Servlet '" + getServletName() + "'");
  5. }
  6. long startTime = System.currentTimeMillis();
  7.  
  8. try {
  9. // 初始化 webApplicationContext
  10. this.webApplicationContext = initWebApplicationContext();
  11. // 在容器被加载后执行,由子类来实现一些必要的初始化
  12. initFrameworkServlet();
  13. }
  14. catch (ServletException | RuntimeException ex) {
  15. logger.error("Context initialization failed", ex);
  16. throw ex;
  17. }
  18. // 略去打印日志的部分
  19. ...
    }

initWebApplicationContext() 方法会初始化并返回一个容器:

  1. protected WebApplicationContext initWebApplicationContext() {
  2. // 获取 Root WebApplicationContext
  3. WebApplicationContext rootContext =
  4. WebApplicationContextUtils.getWebApplicationContext(getServletContext());
  5. WebApplicationContext wac = null;
  6.  
  7. if (this.webApplicationContext != null) {
  8. // A context instance was injected at construction time -> use it
  9. // 一个上下文已经被注入进来
  10. wac = this.webApplicationContext;
  11. if (wac instanceof ConfigurableWebApplicationContext) {
  12. // 如果是 ConfigurableWebApplicationContext,
  13. ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
  14. if (!cwac.isActive()) {
  15. // 没有激活,设置父容器,配置并且刷新容器
  16. if (cwac.getParent() == null) {
  17. cwac.setParent(rootContext);
  18. }
  19. configureAndRefreshWebApplicationContext(cwac);
  20. }
  21. }
  22. }
  23. if (wac == null) {
  24. // 尝试从 ServletContext 中获取一个容器
  25. wac = findWebApplicationContext();
  26. }
  27. if (wac == null) {
  28. // 创建一个新的容器并初始化
  29. wac = createWebApplicationContext(rootContext);
  30. }
  31.  
  32. if (!this.refreshEventReceived) {
  33. // 没有触发过刷新时间
  34. synchronized (this.onRefreshMonitor) {
  35. // 手动触发刷新事件
  36. onRefresh(wac);
  37. }
  38. }
  39.  
  40. if (this.publishContext) {
  41. // Publish the context as a servlet context attribute.
  42. // 将容器发布到 ServletContext 的属性上
  43. String attrName = getServletContextAttributeName();
  44. getServletContext().setAttribute(attrName, wac);
  45. }
  46.  
  47. return wac;
  48. }

onRefresh() 方法供子类来重写, DispatcherServlet 重写了这个方法来初始化 MVC 中的一些组件:

  1. @Override
  2. protected void onRefresh(ApplicationContext context) {
  3. initStrategies(context);
  4. }
  5.  
  6. protected void initStrategies(ApplicationContext context) {
  7. initMultipartResolver(context);
  8. initLocaleResolver(context);
  9. initThemeResolver(context);
  10. initHandlerMappings(context);
  11. initHandlerAdapters(context);
  12. initHandlerExceptionResolvers(context);
  13. initRequestToViewNameTranslator(context);
  14. initViewResolvers(context);
  15. initFlashMapManager(context);
  16. }

initWebApplicationContext() 方法调用的其他方法其实和 ContextLoader 中的方法比较类似, 这里就不再放上来了, 有兴趣的可以访问我的源码注释.

总结

通过本篇博客以及上一篇博客,相信大家对springmvc的上下文有了明确的认识。总的来说,默认springmvc项目会有两个上下文(root webapplicationcontext 和 servlet webapplicationcontext)。接下来的博文会带大家认识一下springboot项目的上下文以及springmvc项目上下文和springboot项目上下文的异同点,敬请期待。

转自:https://www.cnblogs.com/FJH1994/p/10813687.html

Spring MVC源码——Servlet WebApplicationContext的更多相关文章

  1. Spring MVC源码——Root WebApplicationContext

    目录 Spring MVC源码--Root WebApplicationContext 上下文层次结构 Root WebApplicationContext 初始化和销毁 ContextLoaderL ...

  2. 精尽Spring MVC源码分析 - WebApplicationContext 容器的初始化

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  3. 精尽Spring MVC源码分析 - 文章导读

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  4. Spring mvc源码分析系列--Servlet的前世今生

    Spring mvc源码分析系列--Servlet的前世今生 概述 上一篇文章Spring mvc源码分析系列--前言挖了坑,但是由于最近需求繁忙,一直没有时间填坑.今天暂且来填一个小坑,这篇文章我们 ...

  5. 精尽Spring MVC源码分析 - 寻找遗失的 web.xml

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  6. 精尽Spring MVC源码分析 - 一个请求的旅行过程

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  7. 精尽Spring MVC源码分析 - MultipartResolver 组件

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  8. 精尽Spring MVC源码分析 - HandlerMapping 组件(一)之 AbstractHandlerMapping

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  9. 精尽Spring MVC源码分析 - HandlerMapping 组件(二)之 HandlerInterceptor 拦截器

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

随机推荐

  1. hdoj--5562--Clarke and food(模拟)

    Clarke and food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. HBase框架基础(四)

    * HBase框架基础(四) 上一节我们介绍了如何使用HBase搞一些MapReduce小程序,其主要作用呢是可以做一些数据清洗和分析或者导入数据的工作,这一节我们来介绍如何使用HBase与其他框架进 ...

  3. [转]A reference to a higher version or incompatible assembly cannot be added to the project

    If you face this error message while adding a reference to your project in Visual Studio try the fol ...

  4. Windows Server 2012 r2 显示计算机图标

    在 Windows Server 2012 R2 系统中,微软取消了服务器桌面个性化选项,如何重新调出配置界面,在桌面上显示计算机图标,本文为大家介绍一下! Win2012我的电脑怎么显示到桌面? 一 ...

  5. POJ-2785 Values whose Sum is 0 Hash表

    题目链接:https://cn.vjudge.net/problem/POJ-2785 题意 给出四组数,每组有n个数 现从每组数中取一个数作为a,b,c,d 问有几组这样的a+b+c+d=0 思路 ...

  6. linux命令find命令详解

    find 查找文件 find 哪里 什么类型 什么名字 -maxdepth 最大的深度 查找目录的最大深度    find -maxdepth 1 -type d -type 找什么类型的 f fil ...

  7. 【Codeforces Round #421 (Div. 2) A】Mister B and Book Reading

    [题目链接]:http://codeforces.com/contest/820/problem/A [题意] 每天看书能看v页; 且这个v每天能增加a; 但是v有上限v1; 然后每天还必须往回看t页 ...

  8. C# 字符串 分割 反转 Base64

    "; //字符串 ToBase64 byte[] bytes = Encoding.Default.GetBytes(pwd); pwd = Convert.ToBase64String(b ...

  9. 使用YUM安装ZABBIX监控

    http://blog.csdn.net/aqw123456fdg/article/details/48135477 http://www.cnblogs.com/enjoycode/p/zabbix ...

  10. 二叉树、B树、B+树、B*树、LSM树

      HBase 对于数据产品,底层存储架构直接决定了数据库的特性和使用场景.RDBMS(关系型数据库)使用 B树 及 B+树 作为数据存储结构. HBase 使用 LSM树. .     二叉树    ...