DispatchServlet作為springmvc核心類,以下是它的結構圖
Aware接口,比如實作了
ApplicationContextAware
接口的類,能夠擷取到
ApplicationContext。
DispatcherServer的構造器
先初始化父類的,然後設定一個父類屬性的值,将http請求直接發送到doService方法
public DispatcherServlet() {
super();
//設定屬性,是否将http請求發送到doService方法
setDispatchOptionsRequest(true);
}
public DispatcherServlet(WebApplicationContext webApplicationContext) {
super(webApplicationContext);
setDispatchOptionsRequest(true);
}
FrameworkServlet的構造器,繼續對父類的HttpServletBean類進行建構
public FrameworkServlet() {
}
public FrameworkServlet(WebApplicationContext webApplicationContext) {
this.webApplicationContext = webApplicationContext;
}
HttpServletBean中的init初始化方法
init中調用了兩個模闆方法,可以由子類去實作
initServletBean就是子類FrameworkServlet的入口方法
@Override
public final void init() throws ServletException {
if (logger.isDebugEnabled()) {
logger.debug("Initializing servlet '" + getServletName() + "'");
}
//将servlet的參數封裝到pvs中,requiredProperties是必須參數
PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
if (!pvs.isEmpty()) {
try {
//建立并給DispatchServlet指派
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) {
if (logger.isErrorEnabled()) {
logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
}
throw ex;
}
}
//調用子類方法
initServletBean();
if (logger.isDebugEnabled()) {
logger.debug("Servlet '" + getServletName() + "' configured successfully");
}
}
protected void initBeanWrapper(BeanWrapper bw) throws BeansException {
}
protected void initServletBean() throws ServletException {
}
FrameworkServlet的initServletBean的方法
initFrameworkServlet也是一個模闆方法,由子類提供實作,不過子類沒實作
onRefresh模闆方法,子類提供
@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 | 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() {
//擷取rootContext
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
//如果已經通過構造方法構造了webApplicationContext
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) {
//當webApplicationContext已經存在servletContext,通過servlet配置的參數進行擷取
wac = findWebApplicationContext();
}
if (wac == null) {
//不存在,則創一個webApplicationContext
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
//當ContextRefreshedEvent沒有觸發時調用此方法
//子類DispatchServlet類方法的入口
onRefresh(wac);
}
if (this.publishContext) {
//将applicationContext放入ServletContext
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;
}
protected void initFrameworkServlet() throws ServletException {
}
DispatcherServlet的onRefresh方法對自己的九大元件進行初始化
@Override
protected void onRefresh(ApplicationContext context) {
initStrategies(context);
}
protected void initStrategies(ApplicationContext context) {
//初始化DispatchServlet的九大元件
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
private void initMultipartResolver(ApplicationContext context) {
//假設有配置多部分檔案傳輸
try {
this.multipartResolver = context.getBean(MULTIPART_RESOLVER_BEAN_NAME, MultipartResolver.class);
if (logger.isDebugEnabled()) {
logger.debug("Using MultipartResolver [" + this.multipartResolver + "]");
}
}
catch (NoSuchBeanDefinitionException ex) {
// 預設是沒有
this.multipartResolver = null;
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate MultipartResolver with name '" + MULTIPART_RESOLVER_BEAN_NAME +
"': no multipart request handling provided");
}
}
}
private void initThemeResolver(ApplicationContext context) {
try {
//再context中擷取,隻要在配置檔案中配置了bean,就能找到
this.themeResolver = context.getBean(THEME_RESOLVER_BEAN_NAME, ThemeResolver.class);
if (logger.isDebugEnabled()) {
logger.debug("Using ThemeResolver [" + this.themeResolver + "]");
}
}
catch (NoSuchBeanDefinitionException ex) {
// 假設沒有,則使用預設是主題解析類
//再dispatch.properties可知,預設的主題解析類org.springframework.web.servlet.theme.FixedThemeResolver
this.themeResolver = getDefaultStrategy(context, ThemeResolver.class);
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate ThemeResolver with name '" + THEME_RESOLVER_BEAN_NAME +
"': using default [" + this.themeResolver + "]");
}
}
}
小結:
HttpServletBean繼承HttpServlet,是将servlet中的配置的參數映射到相對應的屬性上,FrameworkServlet主要是建立并初始化WebApplicationContext,DispatcherServlet初始化了自身的九大元件,采用了大量的模闆方法,由父類去調用子類的方法。