Spring ServletContextListener源码解析

web项目的核心技术是javax.servlet
记录一下探索spring framework的源码分析
我使用IDEA,搭建一个webappmaven项目,在pom.xml中只添加以下依赖:

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.8.RELEASE</version>
    </dependency>

</dependencies>

简单回顾以下web.xml的配置,在最开始还没有MVC架构的时候,我们需要手动将每个Servlet类添加到web.xml,甚至是自定义的FilterListener也要添加,也就是说tomcat作为web容器,当它要启动一个web项目的时候,会首先去解析web.xml,从而启动我们的应用程序。这里ServletFilterListenerJava Web技术的三大核心技术:

Servlet:一次客户端的请求/响应就是产生一个Servlet,业务处理中的核心类。
Filter:仅项目启动的时候加载一次,从横向的角度对所有请求/响应做预处理,AOP编程思想的技术基础。
Listener:仅项目启动的时候加载一次,可以对应用程序中的对象进行监听,当对象发生初始化、销毁、值更新等待时将触发特定的事件。

这三个相关的技术点都在java.servlet包类,ServletContext类在每一个应用程序中只有实例,ServletContext对象包含在ServletConfig对象中,而ServletConfig对象是tomcatServlet进行初始化时提供给servlet的。因此在程序启动时就可以将一些资源或者对象信息保存在ServletContext作为应用全局共享内存使用,但需要自己提供实现类。

ServletContextListener类负责在tomcat启动和销毁应用程序时将事件通知给实现类,下面是次接口的源码:

package javax.servlet;

import java.util.EventListener;

public interface ServletContextListener extends EventListener {
   
   
    void contextInitialized(ServletContextEvent var1);
    void contextDestroyed(ServletContextEvent var1);
}

现在要依赖spring framework框架来开发,下面是一份简单的web.xml配置:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
	    <servlet-name>DispatcherServlet</servlet-name>
	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
	    <servlet-name>DispatcherServlet</servlet-name>
	    <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

仅仅添加上面的配置是不行的,从日志错误上可以看到是因为ContextLoaderListener类在执行时没有找到applicationContext.xml配置文件,从这里开始追踪源码。
Spring通过一个ContextLoader的类进行应用上下文的初始化(方法:createWebApplicationContext),如果没有从web.xml的配置中读取到初始化参数,就从默认的配置中获取应用上下文的实现类,下面两段源码说明了这一过程:

protected Class<?> determineContextClass(ServletContext servletContext) {
   
   
	String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
	if (contextClassName != null) {
   
   
		try {
   
   
			return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
		}
		catch (ClassNotFoundException ex) {
   
   
			throw new ApplicationContextException(
					"Failed to load custom context class [" + contextClassName + "]", ex);
		}
	}
	else {
   
   
		contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
		try {
   
   
			return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
		}
		catch (ClassNotFoundException ex) {
   
   
			throw new ApplicationContextException(
					"Failed to load default context class [" + contextClassName + 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值