概述
Spring 初始化过程
Spring 初始化过程
- javax.servlet.Servlet#public void init(ServletConfig config) 方法
public interface Servlet {
public void init(ServletConfig config) throws ServletException;
}
-
javax.servlet.GenericServlet 抽象类
public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable { @Override public void init(ServletConfig config) throws ServletException { this.config = config; this.init(); } //空实现 public void init() throws ServletException { // NOOP by default } }
-
javax.servlet.http.HttpServlet
public abstract class HttpServlet extends GenericServlet { ... //没有init方法的实现 }
-
进入Spring的定义 org.springframework.web.servlet.HttpServletBean
public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware { ... @Override public final void init() throws ServletException { // ...省略 // 初始化 initServletBean(); } //空方法没有实现 protected void initServletBean() throws ServletException { //... } }
-
org.springframework.web.servlet.FrameworkServlet
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware { @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 { 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"); } } }
initWebApplicationContext
protected WebApplicationContext initWebApplicationContext() { WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); WebApplicationContext wac = null; 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) { wac = findWebApplicationContext(); } if (wac == null) { wac = createWebApplicationContext(rootContext); } if (!this.refreshEventReceived) { onRefresh(wac); } 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; }
-
onRefresh 方法与initFrameworkServlet方法
protected void onRefresh(ApplicationContext context) { // For subclasses: do nothing by default. } protected void initFrameworkServlet() throws ServletException { }
-
org.springframework.web.servlet.DispatcherServlet
public class DispatcherServlet extends FrameworkServlet { @Override protected void onRefresh(ApplicationContext context) { initStrategies(context); } /** * 初始化策略,之类的组件 * */ protected void initStrategies(ApplicationContext context) { initMultipartResolver(context); initLocaleResolver(context); initThemeResolver(context); initHandlerMappings(context); initHandlerAdapters(context); initHandlerExceptionResolvers(context); initRequestToViewNameTranslator(context); initViewResolvers(context); initFlashMapManager(context); } }
最后
以上就是忧心御姐为你收集整理的Spring 中DispatcherServlet初始化过程的全部内容,希望文章能够帮你解决Spring 中DispatcherServlet初始化过程所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复