Spring MVC(一)Servlet 2.x 规范在 Spring MVC 中的应用

Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html)

《Servlet 2.x 规范》:https://www.cnblogs.com/binarylei/p/10205884.html

Servlet 容器在启动时会调用 ServletContextListener 的 contextInitialized() 方法。同时 Servlet 在初始化时会执行 init 方法。Spring MVC 正是在这两个过程中创建 Root WebApplicationContext 和 Servlet WebApplicationContext 容器。

一、ContextLoaderListener

ContextLoaderListener 的 contextInitialized 只是简单的调用了一个其父类 ContextLoader 的 initWebApplicationContext 方法,创建了一个 Root 容器。

  1. // 创建 Root WebApplicationContext
  2. public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
  3. if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
  4. throw new IllegalStateException(
  5. "Cannot initialize context because there is already a root application context present");
  6. }
  7. try {
  8. // 1. 创建 Root WebApplicationContext
  9. if (this.context == null) {
  10. this.context = createWebApplicationContext(servletContext);
  11. }
  12. // 2. 配置 Root WebApplicationContext
  13. if (this.context instanceof ConfigurableWebApplicationContext) {
  14. ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
  15. if (!cwac.isActive()) {
  16. if (cwac.getParent() == null) {
  17. ApplicationContext parent = loadParentContext(servletContext);
  18. cwac.setParent(parent);
  19. }
  20. configureAndRefreshWebApplicationContext(cwac, servletContext);
  21. }
  22. }
  23. servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
  24. // 3. 保存 context
  25. ClassLoader ccl = Thread.currentThread().getContextClassLoader();
  26. if (ccl == ContextLoader.class.getClassLoader()) {
  27. currentContext = this.context;
  28. } else if (ccl != null) {
  29. currentContextPerThread.put(ccl, this.context);
  30. }
  31. return this.context;
  32. } catch (RuntimeException | Error ex) {
  33. }
  34. }

二、DispatcherServlet

  1. 类继承关系:DispatcherServlet -> FrameworkServlet -> HttpServletBean -> HttpServlet -> Servlet

初始化 DispatcherServlet 时会调用 Servlet 的 init() 方法。

  1. // HttpServletBean#init
  2. @Override
  3. public final void init() throws ServletException {
  4. // 省略...
  5. initServletBean();
  6. }
  7. // FrameworkServlet#initServletBean
  8. @Override
  9. protected final void initServletBean() throws ServletException {
  10. this.webApplicationContext = initWebApplicationContext();
  11. initFrameworkServlet();
  12. }

至此终于看到 Servlet WebApplicationContext 的创建了。

  1. protected WebApplicationContext initWebApplicationContext() {
  2. // 1. 获取父容器
  3. WebApplicationContext rootContext =
  4. WebApplicationContextUtils.getWebApplicationContext(getServletContext());
  5. WebApplicationContext wac = null;
  6. // 2. 子容器已经存在则设置其父容器并启动
  7. if (this.webApplicationContext != null) {
  8. wac = this.webApplicationContext;
  9. if (wac instanceof ConfigurableWebApplicationContext) {
  10. ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
  11. if (!cwac.isActive()) {
  12. if (cwac.getParent() == null) {
  13. cwac.setParent(rootContext);
  14. }
  15. configureAndRefreshWebApplicationContext(cwac);
  16. }
  17. }
  18. }
  19. // 3. 不存在则创建一个新子容器
  20. if (wac == null) {
  21. wac = findWebApplicationContext();
  22. }
  23. if (wac == null) {
  24. wac = createWebApplicationContext(rootContext);
  25. }
  26. if (!this.refreshEventReceived) {
  27. onRefresh(wac);
  28. }
  29. if (this.publishContext) {
  30. String attrName = getServletContextAttributeName();
  31. getServletContext().setAttribute(attrName, wac);
  32. }
  33. return wac;
  34. }

每天用心记录一点点。内容也许不重要,但习惯很重要!

Spring MVC(一)Servlet 2.x 规范在 Spring MVC 中的应用的更多相关文章

  1. cpj-swagger分别整合struts2、spring mvc、servlet

    cpj-swagger 原文地址:https://github.com/3cpj/swagger 1. Swagger是什么? 官方说法:Swagger是一个规范和完整的框架,用于生成.描述.调用和可 ...

  2. 【Java Web开发学习】Spring MVC添加自定义Servlet、Filter、Listener

    [Java Web开发学习]Spring MVC添加自定义Servlet.Filter.Listener 转载:https://www.cnblogs.com/yangchongxing/p/9968 ...

  3. Spring企业级程序设计 • 【第6章 深入Spring MVC开发】

    全部章节   >>>> 本章目录 6.1 模型数据解析及控制器返回值 6.1.1 ModelAndView多种用法 6.1.2  Map添加模型数据和返回String类型值 6 ...

  4. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块

    spring framework中的spring web MVC模块 1.概述 spring web mvc是spring框架中的一个模块 spring web mvc实现了web的MVC架构模式,可 ...

  5. spring boot整合servlet、filter、Listener等组件方式

    创建一个maven项目,然后此项目继承一个父项目:org.springframework.boot 1.创建一个maven项目: 2.点击next后配置父项目及版本号 3.点击finish后就可查看p ...

  6. spring boot(18)-servlet、filter、listener

    servlet.filter.listener的用法就不讲了,只讲如何在spring boot中配置它们.有两种方式,一种是从servlet3开始提供的注解方式,另一种是spring的注入方式 ser ...

  7. Spring Boot 集成servlet,发布为可直接运行的war包,方便后续打包为docker镜像。

    背景:Spring Boot 集成servlet,发布为可直接运行的war包,方便后续打包为docker镜像. 原文地址 https://github.com/weibaohui/springboot ...

  8. Servlet 2.3 规范

    Servlet2.3规范 第一章: servlet2.3规范用到了一下的一些规范:J2EE.JSP1.1.JNDI 在14章中讲述了规范中的所有的classes类或接口(改文中不讲述).对开发者而言以 ...

  9. Spring Boot (19) servlet、filter、listener

    servlet.filter.listener,在spring boot中配置方式有两种:一种是以servlet3开始提供的注解方式,另一种是spring的注入方式. servlet注解方式 serv ...

随机推荐

  1. php优化-》常用到的部分优化

    1.循环内部尽可能不要声明变量: 2.在可以用PHP内部字符串操作函数的情况下,尽量不要用正则表达式: 3.foreach效率更高,尽量用foreach代替while和for循环: 4.用单引号替代双 ...

  2. Spring 7种事务传播行为

    1.PROPAGATION_REQUIRED:如果当前没有事务,就创建一个新事务,如果当前存在事务,就加入该事务,该设置是最常用的设置. 2.PROPAGATION_SUPPORTS:支持当前事务,如 ...

  3. kangle web配置phpmyadmin

    1. kangle安装参考:https://www.kangleweb.com/thread-6001-1-1.html 2. 安装mysql-5.7.22:http://www.cnblogs.co ...

  4. sublime text3的注册码以及常用方法

    Michael BarnesSingle User LicenseEA7E-8213858A353C41 872A0D5C DF9B2950 AFF6F667C458EA6D 8EA3C286 98D ...

  5. 项目没有build path问题(转)

    感谢作者分享:https://blog.csdn.net/u012572815/article/details/76353018 问题1.通过eclipse的svn资源库添加的项目,显示的方式和直接创 ...

  6. oracle 中更新update不成功的原因

    oracle 中执行insert into 与delete 都正常,但是执行update 却没有反应. 原因: 是因为记录锁.这种只有update无法执行其他语句可以执行的其实是因为记录锁导致的,在o ...

  7. :after伪类+content经典应用举例

    :after伪类+content 清除浮动的影响 .box{padding:10px; background:gray;} .l{float:left;} <div class="bo ...

  8. TOJ2811: Bessie's Weight Problem(完全背包)

    传送门(<---可以点的) 描述 Bessie, like so many of her sisters, has put on a few too many pounds enjoying t ...

  9. View 常用方法

    id layout_width layout_height layout_margin.layout_marginTop minWidth minHeight background layout_gr ...

  10. 微信小程序开发——以简单易懂的浏览器页面栈理解小程序的页面路由

    前言: 对于小程序的页面路由,如果没有一定开发经验的话,理解起来还是会有些困难的.哪怕是有一定小程序开发经验的开发者,能够完全理解掌握的恐怕也不多. 这里就以另外一种方式来详细的介绍小程序的页面栈及路 ...