當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring MVC源码解析
生活随笔
收集整理的這篇文章主要介紹了
Spring MVC源码解析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring Mvc結構解析
上圖是Dispatcher Servlet的結構圖,從圖中可以清楚的看到Dispatcher Servlet的繼承鏈,下面我們將基于Spring4.1.6揭開Spring MVC的神秘面紗。
初始化過程
我們都知道,Java中初始化順序是:
那么,在Dispatcher Servlet初始化的時候也是嚴格按照這個順序來執行的。我們分別來看看在不同階段都做了些什么。
HttpServletBean初始化過程關鍵代碼
@Overridepublic final void init() throws ServletException {// 從spring的配置文件中讀取servlet的Init-Param。即spring配置文件路徑等關鍵配置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;}// 調用frameworkServlet的方法初始化initServletBean();}FrameworkServlet初始化過程關鍵代碼
@Overrideprotected final void initServletBean() throws ServletException { this.webApplicationContext = initWebApplicationContext(); }protected WebApplicationContext initWebApplicationContext() {WebApplicationContext rootContext =WebApplicationContextUtils.getWebApplicationContext(getServletContext());WebApplicationContext wac = null;if (wac == null) {// 在這里初始化Spring MVC的context,并將Spring MVC的context和Servlet Context綁定wac = createWebApplicationContext(rootContext);}return wac;}protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {Class<?> contextClass = getContextClass();// 通過反射new一個空的context對象返回ConfigurableWebApplicationContext wac =(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);wac.setEnvironment(getEnvironment());wac.setParent(parent);wac.setConfigLocation(getContextConfigLocation());// 在這里會調用onRefreah方法,初始化Dispatcher Servlet的9大策略configureAndRefreshWebApplicationContext(wac);return wac;}protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {if (ObjectUtils.identityToString(wac).equals(wac.getId())) {// The application context id is still set to its original default value// -> assign a more useful id based on available informationif (this.contextId != null) {wac.setId(this.contextId);}else {wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +ObjectUtils.getDisplayString(getServletContext().getContextPath()) + "/" + getServletName());}}// 為web application context設置基本屬性wac.setServletContext(getServletContext());wac.setServletConfig(getServletConfig());wac.setNamespace(getNamespace());wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));ConfigurableEnvironment env = wac.getEnvironment();if (env instanceof ConfigurableWebEnvironment) {((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());}postProcessWebApplicationContext(wac);applyInitializers(wac);// 關鍵,這里會刷新整個context,調用Dispatcher Servlet的onRefreah方法。初始化一系列Resolverswac.refresh();}DispatcherServlet初始化過程關鍵代碼
/*** This implementation calls {@link #initStrategies}.*/@Overrideprotected void onRefresh(ApplicationContext context) {initStrategies(context);}/*** Initialize the strategy objects that this servlet uses.* <p>May be overridden in subclasses in order to initialize further strategy objects.*/protected void initStrategies(ApplicationContext context) {initMultipartResolver(context);initLocaleResolver(context);initThemeResolver(context);// 尋找配置的HandlerMapping,下篇文章我們會繼續分析這里的詳細過程。是url映射controller的關鍵,默認是RequestMappgingHandlerMapping和BeanNameUrlHandlerMapping,這些HandlerMapping會在spring context初始化的時候初始化。initHandlerMappings(context);// 尋找配置的適配器,默認是RequestMappingHandlerAdapter,HttpRequestHandlerAdapter,SimpleControllerHandlerAdapter這三種initHandlerAdapters(context);initHandlerExceptionResolvers(context);initRequestToViewNameTranslator(context);initViewResolvers(context);initFlashMapManager(context);}從上面的關鍵部分代碼可以看出Spring MVC在啟動時的的大概流程,大致了解Spring MVC都做了什么。后續會繼續分析請求到達Dispatcher Servlet后是如何找到對應的controller以及參數封裝,最后再到數據返回等流程的詳細過程。
總結
以上是生活随笔為你收集整理的Spring MVC源码解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习spring过程看的笔记(一)
- 下一篇: Fragment 退出动画导致fragm