在使用SpringMVC時會在Web.xml配置如下代碼:
<!-- 加入SpringMVC配置 begin-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- end -->
先看看DispatcherServlet類的繼承關系
DispatcherServlet類的繼承關系
作為Servlet,DispatcherServlet的啟動與Servlet的啟動過程是想聯系的,在Servlet啟動的過程中,Servlet的init()方法會被調用,以進行初始化,首先會調用HttpServletBean的init()方法,因為HttpServletBean繼承自HttpServlet
- 運用了依賴注入思想完成了<init-param>配置元素的讀取
public void setContextConfigLocation(String contextConfigLocation) {
this.contextConfigLocation = contextConfigLocation;
}
讀取在web.xml配置的 classpath:/dispatcher-servlet.xml
- HttpServletBean類init()方法
@Override
public final void init() throws ServletException {
if (logger.isDebugEnabled()) {
logger.debug("Initializing servlet '" + getServletName() + "'");
}
// Set bean properties from init parameters.
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;
}
// 讓子類去初始化
initServletBean();
if (logger.isDebugEnabled()) {
logger.debug("Servlet '" + getServletName() + "' configured successfully");
}
}
- initServletBean的初始化過程在FrameworkServlet中完成
@Override
protected final void initServletBean() throws ServletException {
getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
if (this.logger.isInfoEnabled()) {
this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
}
long startTime = System.currentTimeMillis();
try {
//初始化WebApplicationContext上下文
this.webApplicationContext = initWebApplicationContext();
//空實現
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;
}
if (this.logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
elapsedTime + " ms");
}
}
protected WebApplicationContext initWebApplicationContext() {
//調用WebApplicationContextUtils靜態類得到跟上下文,這個跟上下文是保存在ServletContext中的
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
//如果webApplicationContext已經不為空,表示這個Servlet類是通過編程式注冊到容器中的(Servlet 3.0+中的ServletContext.addServlet() ),上下文也由編程式傳入。
//若這個傳入的上下文還沒被初始化,將rootContext上下文設置為它的父上下文,然后將其初始化,否則直接使用
if (this.webApplicationContext != null) {
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
if (cwac.getParent() == null) {
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// 此時以contextAttribute屬性的值為鍵,在ServletContext中查找上下文,查找得到,說明上下文已經以別的方式初始化并注冊在contextAttribute下,直接使用
wac = findWebApplicationContext();
}
if (wac == null) {
//沒有上下文實例定義servlet ->創建一個
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
//刷新上下文
onRefresh(wac);
}
//把當前創建的上下文保存到ServletContext中去,使用的屬性名是和當前Servlet名相關的
if (this.publishContext) {
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
}
//返回當前創建的上下文
return wac;
}
那么這個MVC的上下文就建立起來了,具體取得跟上下文的過程在WebApplicationContextUtils中實現的這個根上下文是在ContextLoader設置到ServletContext中去的,使用的屬性是ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,這個根上下文是DispatherServlet建立的上下文的父上下文,所以根上下管理的Bean也是可以被DispatherServlet的上下文使用的。通過getBean向Ioc容器獲取Bean時,容器會先到他的父Ioc容器中獲得getBean
- WebApplicationContextUtils.getWebApplicationContext
public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
}
public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName) {
Assert.notNull(sc, "ServletContext must not be null");
Object attr = sc.getAttribute(attrName);
if (attr == null) {
return null;
}
if (attr instanceof RuntimeException) {
throw (RuntimeException) attr;
}
if (attr instanceof Error) {
throw (Error) attr;
}
if (attr instanceof Exception) {
throw new IllegalStateException((Exception) attr);
}
if (!(attr instanceof WebApplicationContext)) {
throw new IllegalStateException("Context attribute is not of type WebApplicationContext: " + attr);
}
return (WebApplicationContext) attr;
}
- FrameworkServlet的createWebApplicationContext()方法
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
// getContextClass()--> XmlWebApplicationContext.class
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());
//設置父上下文
wac.setParent(parent);
wac.setConfigLocation(getContextConfigLocation());
//刷新上下文
configureAndRefreshWebApplicationContext(wac);
return wac;
}