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

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

HttpServletBean

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

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

public final void init() throws ServletException {
// Set bean properties from init parameters.
// 从 ServletConfig 中取出初始化参数到 PropertyValues。ServletConfigPropertyValues 的构造器中将会检查是否缺失了必要属性
PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
if (!pvs.isEmpty()) {
try {
// 将 servlet 对象包装成 BeanWrapper ,从而能够以 Spring 的方式(反射)来注入参数
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
// 注册 PropertyEditor,遇到 Resource 类型的属性时,用 ResourceEditor 解析
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
// 初始化 BeanWrapper,空方法
initBeanWrapper(bw);
// 注入属性,忽略没有 setter 的属性
bw.setPropertyValues(pvs, true);
}
catch (BeansException ex) {
if (logger.isErrorEnabled()) {
logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
}
throw ex;
}
}
// Let subclasses do whatever initialization they like.
// 由子类实现初始化逻辑
initServletBean();
}
private static class ServletConfigPropertyValues extends MutablePropertyValues {
public ServletConfigPropertyValues(ServletConfig config, Set<String> requiredProperties)
throws ServletException {
// 将 requiredProperties 拷贝到新的 Set missingProps
Set<String> missingProps = (!CollectionUtils.isEmpty(requiredProperties) ?
new HashSet<>(requiredProperties) : null); // 将 ServletConfig 中的初始化参数取出,添加到 MutablePropertyValues 中
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)) {
// 存在必须出现的条件没出现
throw new ServletException(
"Initialization from ServletConfig for servlet '" + config.getServletName() +
"' failed; the following required properties were missing: " +
StringUtils.collectionToDelimitedString(missingProps, ", "));
}
}
}

FrameworkServlet

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

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

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

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

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

protected WebApplicationContext initWebApplicationContext() {
// 获取 Root WebApplicationContext
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null; if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
// 一个上下文已经被注入进来
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
// 如果是 ConfigurableWebApplicationContext,
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
// 没有激活,设置父容器,配置并且刷新容器
if (cwac.getParent() == null) {
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// 尝试从 ServletContext 中获取一个容器
wac = findWebApplicationContext();
}
if (wac == null) {
// 创建一个新的容器并初始化
wac = createWebApplicationContext(rootContext);
} if (!this.refreshEventReceived) {
// 没有触发过刷新时间
synchronized (this.onRefreshMonitor) {
// 手动触发刷新事件
onRefresh(wac);
}
} if (this.publishContext) {
// Publish the context as a servlet context attribute.
// 将容器发布到 ServletContext 的属性上
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
} return wac;
}

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

@Override
protected void onRefresh(ApplicationContext context) {
initStrategies(context);
} protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}

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. GridView中使用CheckBox

    asp.net中checkbox是向server提交时才干触发事件 把该控件的autopostback设置为true.才干响应事件  protected void Checkbox_CheckedCh ...

  2. PermissionError: [Errno 13] in python

    出现该错误,首先尝试以管理员身份运行 cmd.exe 程序,然后关闭所有的与 python 相关的进程. 1. open 打开一个文件夹(目录),而不是文件 这一错误一般发生在使用 open函数对文件 ...

  3. 5.array

    #include <iostream> #include <array> #include <algorithm> using namespace std; //a ...

  4. 系统丢失的DLL文件问题根源解决(纯净官网下载放心)(图文详解)(博主推荐)

    导言 最近,身边的朋友们,问我,他电脑的win10系统里 mfc110.dll 丢失. 其他的系统文件丢失修复,是一样的步骤. 现象 大家也许,都会有这么一个习惯,动不动则就去百度上搜索. 其实啊,这 ...

  5. java 类和对象2

    编写Java应用程序.首先,定义一个时钟类——Clock,它包括三个int型成员变量分别表示时.分.秒,一个构造方法用于对三个成员变量(时.分.秒) 进行初始化,还有一个成员方法show()用于显示时 ...

  6. 验证备份集-使用DBVERIFY工具

    DBVERIFY确认备份集是否存在坏块 验证TEST03.DBF 文件的是否存在坏块 C:\Documents and Settings\Administrator>dbv file=D:\or ...

  7. 2、go Defer

    package main import ( "fmt" "os") func main() { f:=createFile("D:\\webfront ...

  8. UI Framework-1: Ash Color Chooser

    Ash Color Chooser Overview This document describes how to achieve <input type=”color”> UI in C ...

  9. tf.nn.top_k(input, k, name=None)和tf.nn.in_top_k(predictions, targets, k, name=None)

    tf.nn.top_k(input, k, name=None) 这个函数的作用是返回 input 中每行最大的 k 个数,并且返回它们所在位置的索引. input: 一个张量,数据类型必须是以下之一 ...

  10. 洛谷P2197 nim游戏模板

    Code: #include<iostream> using namespace std; int main(){ int t; cin>>t; while(t--){ int ...