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(