Spring MVC概述
Spring MVC是Spring的一个重要模块,在Web应用中MVC的设计模式已经广为人知,MVC的设计概念如下图所示
MVC模式在UI设计中使用的非常普遍,在Gof的设计模式的经典著作中,开篇就是这个模式。这个模式的额主要特点是分离了模型,视图与控制器三种角色,将业务处理从UI设计中独立出来,封装到模型与控制器设计中去。使得它们相互解耦可以独立扩展。
使用Spring MVC的时候,需要在web.xml中配置DispatcherServlet,这个DispatcherServlet可以看做一个前端控制器的具体实现。还需要在Bean定义配置请求与控制器的对应关系,以及各种视图的展现方式。
应用上下文(ApplicationContext)在web容器中的启动
了解Spring MVC需要首先了解Spring IOC是如何在IOC容器中起效果的。如果要在Web环境适应IOC容器需要为Spring IOC设计一个启动过程,而这个过程是和web容器的启动集成在一起的
。下面以Tomcat这个web容器为例分析IOC容器的启动过程。
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"
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">
<filter>
<filter-name>Spring character encoding filter</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>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在这个部署描述文件中首先定义了一个Servlet对象,这个DispatcherServlet起着分发请求的作用,对它的分析是后面几节的重点但我们现在的关注点在ContextLoaderListener
这个监听器。(<context-param>节点中的内容用来指定Spring IOC容器读取Bean定义的xml文件路径)。ContextLoaderListener监听器负责完成ioc容器在web环境中的启动工作。
DispatcherServlet与ContextLoaderListener提供了web容器对Spring的接口,也就是说这些接口与web容器的耦合是通过ServletContext
实现的。ServletContext为Spring的IOC容器提供了一个宿主环境。
IOC容器启动的基本过程
ioc容器的启动过程就是上下文建立的过程,该上下文与ServletContext相伴相生,由ContextLoaderListener启动的上下文为根上下文,在根上下文的基础上还有一个与Web MVC相关的上下文,
构成一个层次化的上下文体系,具体过程如下
之前已经说了ioc容器的初始化的入口在ContextLoaderListener,这是一个Spring提供的Servlet容器的监听类,它的继承关系如下
WebApplicationContext的设计
为了方便在web环境中使用ioc容器,Spring提供了上下文的扩展接口WebApplicationContext来满足启动过程的需要,它的继承关系如下
我们看看WebApplicationContext接口的定义,只有一个抽象方法属于该接口
/**
* Return the standard Servlet API ServletContext for this application.
*/
@Nullable
ServletContext getServletContext();
通过这个方法可以获得当前Web容器的ServletContext,相当于提供了一个Web容器级别的全局环境。
在启动时Spring会使用默认的XmlWebApplicationContext作为IOC容器,在Web环境中对定位BeanDefination元数据的Resource有特殊的要求,这个要求体现在getConfigLoacation
这个方法中。
XmlWebApplicationContext:loadBeanDefinations
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
reader.loadBeanDefinitions(configLocation);
}
}
}
AbstractRefreshableConfigApplicationContext:getConfigLocations
@Nullable
protected String[] getConfigLocations() {
return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());
}
可以看到当我们不定义时会通过getDefaultConfigLocations
方法加载默认的配置文件。
XmlWebApplicationContext:getDefaultConfigLocations
/** Default config location for the root context */
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
@Override
protected String[] getDefaultConfigLocations() {
if (getNamespace() != null) {
return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};
}
else {
return new String[] {DEFAULT_CONFIG_LOCATION};
}
}
这下明白为什么spring的入门教程都叫你把配置文件取成applicationContext.xml
了吧。(笑)
事实上XmlWebApplicationContext中基本的上下文功能都已经通过继承获得,而这个类在此基础上扩展的就是去何处获取定义BeanDefination的元信息,在获得这些信息后后面的过程就和我们系列文章的第一篇那样使用XmlBeanDefinationReader载入Bean定义信息,最后完成整个上下文的初始化过程的。
ContextLoaderListenre的设计与实现
ContextLoaderListener通过基类ContextLoader完成对WebApplicationContext的初始化,通过实现ServletContextListener完成ServletContext生命周期的回调。之前已经说了ioc容器会随着ServletContext的创建而创建,那么相应的回调方法就是contextInitialized
ContextLoaderListener
/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
可以看到将初始化工作交给了基类ContextLoader去做
ContextLoader:initWebApplicationContext
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
// 判断在Servlet上下文中是否已经有根ioc容器存在
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) {
// 传入ServletContext,创建WebApplicationContext
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);
}
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
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);
//将创建好的ioc容器与ServletContext的ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性绑定
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;
}
}
具体的根容器的创建在createWebApplicationContext
方法中
ContextLoader: createWebApplicationContext
protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
Class<?> contextClass = determineContextClass(sc);
if (!ConfigurableWebAppl