1,Spring Web MVC的逻辑结构
其他Web MVC框架结构类似,通过请求访问驱动,围绕一个中心Servlet工作,该Servlet负责分派请求到控制器Controllers并提供其他功能。
Spring's DispatcherServlet实现这个中心Servlet的角色,即下图中的Front Controller
2,Spring容器的创建
Spring Web MVC自然离不开容器AppliationContext,而Root AppliationContext在Web应用中并不是直接New出来的,而是通过下面方法创建。
在web.xml中有如下定义:
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
contextConfigLocation配置指定了,则在configureAndRefreshWebApplicationContext函数中就会读取,并设置给容器
String initParameter = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (initParameter != null) {
wac.setConfigLocation(initParameter);
}
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener实现了ServletContextListener,而如果ServletContextListener配置在Web容器中,且Web容器(支持Servlet 2.3以上规范)支持Listener,则该ServletContextListener就可以监听Web应用的启动和关闭。
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
private ContextLoader contextLoader;
......
}
ContextLoaderListener利用contextLoader成员(一般就是this), 调用initWebApplicationContext-->createWebApplicationContext,configureAndRefreshWebApplicationContext方法从而完成WebApplicationContext实例创建并保存,这样Spring容器在Web应用启动的时候就自动创建了。
/**
* Initialize the root web application context.
*/
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
if (this.contextLoader == null) {
this.contextLoader = this;
}
this.contextLoader.
initWebApplicationContext(event.getServletContext());
}
/**
* Close the root web application context.
*/
public void contextDestroyed(ServletContextEvent event) {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
Spring容器WebApplicationContext创建后会设置在该Web应用ServletContext的属性中
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
3,DispatcherServlet的创建
Root Application Context创建和初始化完毕后就会创建DispatcherServlet
在web.xml中有如下定义:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
可以看出DispatcherServlet是通过load-on-startup方式实现在Web应用启动时由Servlet容器自动创建,去掉load-on-startup配置,则可以延迟到第一次请求创建
需注意Servlet会对应一个ApplicationContext,DispatcherServlet创建的时候,在initServletBean方法中也会创建一个AppliationContext与之对应,正是该ApplicationContext会扫描并创建下面的Controller,而Root ApplicationContext则是该ApplicationContext的父亲
![]()
4,Controller的创建
Spring Web MVC的Controller通过Annotation方式,由Spring容器自动扫描创建
@Controller
public class HomeController {
......
}
且在appServlet/servlet-context.xml配置文件中,有如下相关配置
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<context:component-scan base-package="com.rain.mvcdemo" />