初始化过程
- 在启动Web项目时,容器(Tomcat)会读web.xml中的两个节点和。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- 容器会创建一个ServletContext(上下文), 作为servlet和容器之间的桥梁,整个WEB项目都能使用这个上下文。
- 容器会将读取到转化为键值对,并交给上下文。
- 容器根据加载spring的监听器实例ContextLoaderListener, 在监听到ServletContext上下文改变的时候,运行监听器的
contextInitialized(ServletContextEvent event)
方法,开始初始化Spring的上下文实例WebApplicationContext。 - 同时在这个方法中通过
event.getServletContext().getInitParameter("contextConfigLocation")
来得到context-param 设定的值。 - spring加载器会取key=’contextConfigLocation’, 的参数,然后去加载咱们配置的各种组件(spring data,数据库配置等等)。
- 加载过滤器filter和servlet
由上面的初始化过程可知容器对于web.xml的加载过程是context-param >> listener >> filter >> servlet
配置源码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springEncoding</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>springEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>StickyCaptcha</servlet-name>
<servlet-class>cn.apiclub.captcha.servlet.StickyCaptchaServlet</servlet-class>
<init-param>
<param-name>width</param-name>
<param-value>250</param-value>
</init-param>
<init-param>
<param-name>height</param-name>
<param-value>75</param-value>
</init-param>
<init-param>
<param-name>ttl</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>StickyCaptcha</servlet-name>
<url-pattern>/captcha.leaf</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Spring MVC Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-web.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Servlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Spring MVC Servlet</servlet-name>
<url-pattern>*.fn</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Spring MVC Servlet</servlet-name>
<url-pattern>*.page</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Spring MVC Servlet</servlet-name>
<url-pattern>*.leaf</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class> org.springframework.security.util.FilterToBeanProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>springSecurityFilterChain</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<welcome-file-list>
<welcome-file>main.page</welcome-file>
</welcome-file-list>
</web-app>