一、加载web.xml
1、web.xml 配置spring的配置文件路径
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-basic.xml</param-value>
</context-param>
2、配置Spring监听器。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
二、运行ContextLoaderListener
该监听器实现了ServletContextListener(上下文监听器,当Tomcat容器启动的时候 会调用ServletContextListener里面的contextInitialized方法)
ContextLoaderListener源码
1、判断spring上下文是否已经保存到ServletContext 。(相当于判断是否已经创建过spring上下文)
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}
2、创建Spring上下文对象
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
3、加载WebApplicationContext 的实现类的类信息用于创建Spring上下文。
protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
Class<?> contextClass = determineContextClass(sc);
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
}
return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
}
默认的WebApplicationContext 是 XmlWebApplicationContext 。可以在ContextLodar.properties 里面找到。如果不使用默认的XmlWebApplicationContext 的话。可以在web.xml里面 配置参数contextClass来指定需要使用的WebApplicationContext类名。
如果使用自己实现的WenApplicationContext的话 需要ConfigurableWebApplicationContext接口。
找到类信息后 利用BeanUtils.instantiateClass()。根据类加载机制创建对象实例。
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 + "]", ex);
}
}
}
Assert.notNull(ctor, "Constructor must not be null");
try {
ReflectionUtils.makeAccessible(ctor);
return ctor.newInstance(args);
}
获取ServletContext 里面的contextConfigLocation值。该值是设置在web.xml里面的spring配置文件路径。
String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (configLocationParam != null) {
wac.setConfigLocation(configLocationParam);
}
如果没有在web.xml里面设置的话 XmlWebApplicationContext.class 里面定义了默认值
/** Default config location for the root context */
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
将创建好的spring上下文放到servletContext
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
三、获取Spring上下文
spring 提供了 WebApplicationContextUtils工具类 通过servletContext获取WebApplicationContext