web
项目的核心技术是javax.servlet
记录一下探索spring framework
的源码分析
我使用IDEA
,搭建一个webapp
的maven
项目,在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
,甚至是自定义的Filter
和Listener
也要添加,也就是说tomcat
作为web
容器,当它要启动一个web
项目的时候,会首先去解析web.xml
,从而启动我们的应用程序。这里Servlet
、Filter
、Listener
是Java Web
技术的三大核心技术:
Servlet:一次客户端的请求/响应就是产生一个Servlet,业务处理中的核心类。
Filter:仅项目启动的时候加载一次,从横向的角度对所有请求/响应做预处理,AOP编程思想的技术基础。
Listener:仅项目启动的时候加载一次,可以对应用程序中的对象进行监听,当对象发生初始化、销毁、值更新等待时将触发特定的事件。
这三个相关的技术点都在java.servlet
包类,ServletContext
类在每一个应用程序中只有实例,ServletContext
对象包含在ServletConfig
对象中,而ServletConfig
对象是tomcat
在Servlet
进行初始化时提供给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 +