我们真正意义上掌握一个框架是在能够分析其核心源码的基础之上的,而一个框架的启动过程是其中最基础的核心代码之一,本文我们就从源码分析SpringMVC启动过程。
1.由于Spring MVC是一个建立在Spring框架基础之上的一个web层框架,它负责发送每个请求到合适的处理程序,所以分析Spring MVC启动过程需要配置web.xml。下面是常见的Spring MVC的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">
<display-name>CtrTimeOut</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
# spring的配置
<param-value>classpath:config/spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
# springmvc的配置
<param-value>classpath:config/spring/spring-controller.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.下面我们来看第二部分源码,ContextLoadListener创建WebApplicationContext作为spring的容器上下文。
#org.springframework.web.context.ContextLoader
/**
* 根据xml配置创建applicationContext
*/
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
...
try {
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
if (this.context == null) {//判空 (以注解方式配置时非空)
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) {
...
//读取contextConfigLocation配置并refresh()
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
//将applicationContext设置到servletContext中
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
...
return this.context;
}
}
DispatcherServlet创建WebApplicationContext作为springmvc的上下文 并将ContextLoadListener创建的上下文设置为自身的parent
DispatcherServlet extends FrameworkServlet
#org.springframework.web.servlet.FrameworkServlet
@Override
protected final void initServletBean() throws ServletException {
...
try {
this.webApplicationContext = initWebApplicationContext();
initFrameworkServlet();
}
...
}
protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
...
if (wac == null) {
//创建applicationContext
wac = createWebApplicationContext(rootContext);
}
...
return wac;
}
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
//XmlWebApplicationContext
Class contextClass = getContextClass();
...
//创建applicationContext
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
wac.setEnvironment(getEnvironment());
//设置parent(ContextLoadListener中创建的applicationContext)
wac.setParent(parent);
//读取contextConfigLocation配置
wac.setConfigLocation(getContextConfigLocation());
//refresh()
configureAndRefreshWebApplicationContext(wac);
return wac;
}
3.Spring MVC的applicationContext会去读取配置文件,我们来看一个最简单的配置文件。
#springmvc容器扫描路径
#spring4新增的标签 主要是添加了默认的HandleMappin,ViewResolver,HandleAdapter
经过以上几段复杂的源码分析springMVC启动过程,对于Spring MVC通过源码完成整个SpringMVC框架的启动过程应该有所掌握了吧,启动过程看起来只有三步,但是涉及到的代码数量还是不容小觑的。这些复杂冗长的源码为我们带来了大量的信息,使得我们能够完成对了整个SpringMVC框架的启动过程的分析。想要了解和学习Spring MVC框架的更多源码可以观看本站的Spring MVC视频教程,这里有全面的Spring MVC源码分析分享给你!
QCode09-04 14:38
Code大师09-04 14:50
不写代码你养我啊08-23 11:14
不写代码你养我啊09-17 18:02
要学习了06-18 18:13