Spring 中DispatcherServlet初始化过程

Spring 初始化过程

在这里插入图片描述

Spring 初始化过程

  1. javax.servlet.Servlet#public void init(ServletConfig config) 方法
public interface Servlet {
  public void init(ServletConfig config) throws ServletException;
}
  1. 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
        }       
       }
    
  2. javax.servlet.http.HttpServlet

    public abstract class HttpServlet extends GenericServlet {
        ...
            
            //没有init方法的实现
    }
    
  3. 进入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 {
            //...
    	}
    }
    
  4. 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 {
    	}
    
  1. 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);
    	}
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值