SpringMVC解析(1)-容器初始化

这篇博客详细介绍了SpringMVC项目的容器初始化过程,从web.xml配置开始,讲解了Root WebApplicationContext的初始化,包括ContextLoaderListener的构造方法和初始化过程,以及ContextLoader的类属性和初始化逻辑。接着,文章探讨了DispatcherServlet的Servlet WebApplicationContext,涵盖HttpServletBean的初始化和FrameworkServlet的属性及配置刷新方法。
摘要由CSDN通过智能技术生成

Spring MVC(1) - 容器初始化

spring mvc 项目

web.xml

<!-- Spring配置 会初始化一个Root Spring WebApplicationContext 容器  -->
<!-- 在 Servlet 容器启动时(Tomcat、Jetty)会被 ContextLoaderListener 监听到,从而调用 #contextInitialized(ServletContextEvent event) 方法,初始化 Root WebApplicationContext 容器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/applicationContext.xml</param-value>
</context-param>

<!-- ====================================== -->

<!-- 一个 javax.servlet.http.HttpServlet 对象,除了拦截我们制定的 *.do 请求外,也会初始化一个属于它的 Spring WebApplicationContext 容器。并且,这个容器是以 ContextLoaderListener中初始化的Root 容器作为父容器 -->
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value> // 默认
    </init-param>
    -->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

Root WebApplicationContext

ContextLoaderListener

概述

实现 ServletContextListener 接口,继承 ContextLoader 类,实现 Servlet 容器启动和关闭时,分别初始化和销毁 WebApplicationContext 容器

创建的application context将会在ServletContext中注册 ->(WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)

类结构
类初始化
构造方法
public ContextLoaderListener() {
   }
public ContextLoaderListener(WebApplicationContext context) {
   super(context);
初始化
public void contextInitialized(ServletContextEvent event) {
   
	//ContextLoader#initWebApplicationContext(ServletContext servletContext),初始化 WebApplicationContext 对象	
    initWebApplicationContext(event.getServletContext());
}

ContextLoader

概述

真正实现初始化和销毁 WebApplicationContext 容器的逻辑的类

代码说明
类属性
	//WebApplicationContext id,序列化ID属性名
	public static final String CONTEXT_ID_PARAM = "contextId";
	//servlet context name属性名
	public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
	//实现类配置属性名
	public static final String CONTEXT_CLASS_PARAM = "contextClass";
	//初始化类属性名
	public static final String CONTEXT_INITIALIZER_CLASSES_PARAM = "contextInitializerClasses";
	//全局初始化类属性名
	public static final String GLOBAL_INITIALIZER_CLASSES_PARAM = "globalInitializerClasses";
	//分隔符
	private static final String INIT_PARAM_DELIMITERS = ",; \t\n";
	//类路径资源文件名,定义了默认的策略(默认实现类)
//org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
	private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";

//默认策略属性信息
	private static final Properties defaultStrategies;

	static {
   
	//初始化策略属性信息
		try {
   
			ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
			defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
		}
		catch (IOException ex) {
   
			throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
		}
	}

类初始化
构造函数
public ContextLoader(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值