spring mvc源码之-Servlet初始化(一)

一个spring mvc的项目一般需要配置web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  	<display-name>zhibing_mybatis</display-name>
  
  	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
  	</context-param>
	
	<filter>
		<filter-name>encodingFilter</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>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<listener>
		<description>spring listener</description>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<servlet>
		<description>spring mvc servlet</description>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<description>springmvc config</description>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
    <welcome-file-list>
    	<welcome-file>index.html</welcome-file>
    	<welcome-file>index.htm</welcome-file>
    	<welcome-file>index.jsp</welcome-file>
  	</welcome-file-list>
</web-app>

初始化过程:

1. 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>。

2. 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。

3. 接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。

4. 容器创建<listener></listener>中的类实例,即创建监听

   (备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。

  在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,

  在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。

  在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。

得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。

由上面的初始化过程可知容器对于web.xml的加载过程是context-param >> listener  >> fileter  >> servlet

注意事项:Servlet中init-param与context-param的区别
          <init-parm>配置在<servlet>标签中,用来初始化当前的Servlet的,属于当前Servlet的配置,因此存放在servletConfig对象中;

           通过getServletConfig().getInitParameter("initParam")的方式获取;

          <context-param>直接配置在web.xml的<web-app>标签中,属于上下文参数,在整个web应用中都可以使用,它是全局的,

           因此存放在servletContext对象中(即application对象);

           通过getServletContext().getInitParameter("contextParam")的方式获取;

初始化监听过程:

ContextLoaderListener实现了ServletContextListener接口,ServletContextListener是Java EE标准接口之一,类似tomcat,jetty的java容器启动时便会触发该接口的contextInitialized。

1 、java容器启动触发ContextLoaderListener的contextInitialized

2、contextInitialized 方法调用ContextLoader的initWebApplicationContext方法。

3、initWebApplicationContext调用createWebApplicationContext方法

4、createWebApplicationContext 调用determineContextClass方法

5、determineContextClass有如下代码

其中ContextLoader 类中有段静态代码

ContextLoader.properties 文件内容如下:

至此,determineContextClass方法返回的是XmlWebApplicationContext

6、回到 initWebApplicationContext 方法,调用configureAndRefreshWebApplicationContext方法

7、configureAndRefreshWebApplicationContext 调用了AbstractApplicationContext的refresh方法

8、refresh 方法调用了obtainFreshBeanFactory

9、obtainFreshBeanFactory 调用了AbstractRefreshableApplicationContext类的refreshBeanFactory方法

注意:XmlWebApplicationContext 继承 AbstractRefreshableApplicationContext类。类图如下

10、refreshBeanFactory调用了XmlWebApplicationContext的loadBeanDefinitions

11、loadBeanDefinitions中加载了对应的applicationContext.xml

终上是applicationContext上下文的初始化,接下来进行spring DispatcherServlet初始化过程。即servlet初始化

猜你喜欢

转载自blog.csdn.net/liangkun_java/article/details/81179240