在分析SpringMVC容器之前,我们先了解下ServletContext和ServletContextListener。
什么是ServletContext?
ServletContext代表一个Web应用程序的上下文,程序开始时创建,程序结束时销毁,ServletContext是一个全局信息空间,一个程序只有一个ServletContext,ServletContext可以用来存储属性,获取资源url等,ServletContext接口由Servlet容器实现。
什么是ServletContextListener?
ServletContextListener是用来监听ServletContext生命周期变化的,程序启动时,ServletContextListener的contextInitialized方法会被调用,可以在里面创建好缓存;程序关闭时,ServletContextListener的contextDestroyed方法会被调用,可以在里面保存缓存信息。
让我们看下web.xml配置:
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>log4jConfig</param-name>
<param-value>classpath:/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- ContextLoaderListener继承ServletContextListener用于监听ServletContext变化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Servlet初始化配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
在web.xml配置文件中,有两个主要的配置:ContextLoaderListener和DispatcherServlet。同样的关于spring配置文件的相关配置也有两部分:context-param中的contextConfigLocation和DispatcherServlet中的init-param。
在SpringMVC中,Spring Context是以父子的继承结构存在的。Web环境中存在一个Root Context,这个Context是整个应用的根上下文,是其他context的parent Context。同时SpringMVC也对应的持有一个独立的Servlet Context,它是Root Context的子上下文,为了区别于ServletContext,后面统称为MVC Context。两者都是WebApplicationContext,MVC Context可以调用Root Context配置,反过来则不行。ServletContext则为Root Context和MVC Context提供了宿主环境,而ServletContext则由Servlet容器提供。
- Root Context是在ContextLoaderListener中配置的,当Web应用程序创建ServletContext后,会调用ContextLoaderListener的contextInitialized方法,ContextLoaderListener读取context-param中的contextConfigLocation指定的配置文件,默认为/WEB-INF/applicationContext.xml文件,创建Root Context。Root Context主要负责创建业务有关的对象,Dao, 事务等。
- MVC Context是在DispatcherServlet中配置的,当Web应用程序启动后会调用DispatcherServlet父类HttpServletBean的init方法,FrameworkServlet则实现了此方法,FrameworkServlet读取DispatchServlet中的init-param中的contextConfigLocation指定的配置文件,默认为/WEB-INF/[servlet-name]-servlet.xml文件,创建MVC Context。MVC Context负责创建Servlet相关的组件,如Controller, ViewResolver等。
SpringMVC启动过程大致分为两个过程:
- ContextLoaderListener初始化,实例化Root IOC容器,并注册到ServletContext中;
- DispatcherServlet初始化,实例化MVC IOC容器作为Root IOC的子容器,并注册到ServletContext中;
主要涉及到WebApplicationContext, XmlWebApplicationContext, ContextLoaderListener,
ContextLoader, DispatcherServlet, FrameworkServlet几个类。
启动过程:
1. WepApplicationContext扩展ApplicationContext接口添加对Web环境支持
public interface WebApplicationContext extends ApplicationContext {
String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
String SCOPE_REQUEST = "request";
String SCOPE_SESSION = "session";
String SCOPE_GLOBAL_SESSION = "globalSession";
String SCOPE_APPLICATION = "application";
String SERVLET_CONTEXT_BEAN_NAME = "servletContext";
String CONTEXT_PARAMETERS_BEAN_NAME = "contextParameters";
String CONTEXT_ATTRIBUTES_BEAN_NAME = "contextAttributes";
ServletContext getServletContext();
}
2. ContextLoaderListener继承ServletContextListener创建Root Context
1) ContextLoaderListener继承ServletContextListener,通过initWebApplicationContext初始化Root Context
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
2) ContextLoader初始化Root Context,其中包括entity, dao, service等组件的初始化,并将Root Context注册到ServletContext中,以便后面MVC Context调用Root Context中的组件。
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
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!");
}
Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
if (this.context == null) {
// 创建Root Context XmlWebApplicationContext
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent ->
// determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
cwac.setParent(parent);
}
// applicationContext配置加载并且刷新Root Context
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
// 将context放到servletContext中WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性下
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
}
else if (ccl != null) {
currentContextPerThread.put(ccl, this.context);
}
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}
3) ContextLoader加载contextConfigLocation配置并刷新容器
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
// The application context id is still set to its original default value
// -> assign a more useful id based on available information
String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
if (idParam != null) {
wac.setId(idParam);
}
else {
// Generate default id...
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(sc.getContextPath()));
}
}
wac.setServletContext(sc);
String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (configLocationParam != null) {
// 加载contextConfigLocation配置
wac.setConfigLocation(configLocationParam);
}
// The wac environment's #initPropertySources will be called in any case when the context
// is refreshed; do it eagerly here to ensure servlet property sources are in place for
// use in any post-processing or initialization that occurs below prior to #refresh
ConfigurableEnvironment env = wac.getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
}
customizeContext(sc, wac);
wac.refresh();
}
3. DispatcherServlet继承HttpServlet创建MVCContext
1) HttpServletBean继承HttpServlet,根据Servlet规范,Web容器调用它的init方法进行初始化
public final void init() throws ServletException {
if (logger.isDebugEnabled()) {
logger.debug("Initializing servlet '" + getServletName() + "'");
}
// 1. 设置servlet初始化参数到组件上
try {
PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
initBeanWrapper(bw);
bw.setPropertyValues(pvs, true);
} catch (BeansException ex) {
logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
throw ex;
}
// 2. 提供给子类的扩展点
initServletBean();
if (logger.isDebugEnabled()) {
logger.debug("Servlet '" + getServletName() + "' configured successfully");
}
}
2) FrameworkServlet继承HttpServletBean通过initServletBean初始化MVC Context
protected final void initServletBean() throws ServletException {
/*省略部分代码*/
try {
// 1. 初始化Web上下文
this.webApplicationContext = initWebApplicationContext();
// 2. 提供给子类扩展点
initFrameworkServlet();
}
catch (ServletException ex) {
this.logger.error("Context initialization failed", ex);
throw ex;
}
catch (RuntimeException ex) {
this.logger.error("Context initialization failed", ex);
throw ex;
}
/*省略部分代码*/
}
3) FrameworkServlet通过initWebApplicationContext方法初始化MVC context
protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent -> set
// the root application context (if any; may be null) as the parent
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// No context instance was injected at construction time -> see if one
// has been registered in the servlet context. If one exists, it is assumed
// that the parent context (if any) has already been set and that the
// user has performed any initialization such as setting the context id
wac = findWebApplicationContext();
}
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
// 创建mvc context并设置root context为parent context
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
onRefresh(wac);
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
// 将context加到servletContext中
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
}
return wac;
}
4) FrameworkServlet 加载配置创建MVC Context,并将Root Context设为自己的parent Context,从而可以调用Root Context中的组件
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
Class<?> contextClass = getContextClass();
if (this.logger.isDebugEnabled()) {
this.logger.debug("Servlet with name '" + getServletName() +
"' will try to create custom WebApplicationContext context of class '" +
contextClass.getName() + "'" + ", using parent context [" + parent + "]");
}
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException(
"Fatal initialization error in servlet with name '" + getServletName() +
"': custom WebApplicationContext class [" + contextClass.getName() +
"] is not of type ConfigurableWebApplicationContext");
}
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
wac.setEnvironment(getEnvironment());
// 设置root context为parent context
wac.setParent(parent);
wac.setConfigLocation(getContextConfigLocation());
configureAndRefreshWebApplicationContext(wac);
return wac;
}
5) FrameworkServlet 刷新mvc容器
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
// The application context id is still set to its original default value
// -> assign a more useful id based on available information
if (this.contextId != null) {
wac.setId(this.contextId);
}
else {
// Generate default id...
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(getServletContext().getContextPath()) + "/" + getServletName());
}
}
wac.setServletContext(getServletContext());
wac.setServletConfig(getServletConfig());
wac.setNamespace(getNamespace());
wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));
// The wac environment's #initPropertySources will be called in any case when the context
// is refreshed; do it eagerly here to ensure servlet property sources are in place for
// use in any post-processing or initialization that occurs below prior to #refresh
ConfigurableEnvironment env = wac.getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
}
postProcessWebApplicationContext(wac);
applyInitializers(wac);
// 容器刷新时会触发FrameworkServlet onRefresh方法
wac.refresh();
}
6) DispatcherServlet继承FrameworkServlet实现onRefresh()方法提供前端配置
@Override
protected void onRefresh(ApplicationContext context) {
initStrategies(context);
}
/**
* Initialize the strategy objects that this servlet uses.
* <p>May be overridden in subclasses in order to initialize further strategy objects.
*/
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
// 初始化映射包括Controller和静态资源等
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
从启动过程中我们可以发现,Root Context作为MVC Context的parent context存在,作为子容器的MVC Context可以调用Root Context中的配置,反过来则不行。从启动过程中也可以看出在配置如果在Root Context中配置Controller会导致Service事务失效,因为此时的Service事务还是未装配事务的,只有把Controller放到MVC Context中配置。