昨天買了本淘寶大牛寫的書-《深刻分析java web 技術內幕》,從今天開始,記錄下本身的總結。 java
因爲本人是使用spring MVC開發的,全部就先看了下spring MVC 工做機制與設計模式這章,感受看了以後受益不淺啊!!! web
在一個工程中若是想要使用 spring MVC的話,只須要兩個步驟 spring
須要配置一個org.springframework.web.servlet.DispatcherServlet的servlet。 設計模式
在這個配置文件裏面咱們只須要擴展一個路徑映射關係,定義一個視圖解析器,再定義一個業務邏輯的處理流程規則。 app
這樣就能夠搞定一個最基本的Spring MVC的應用了。 框架
--------------------------邪惡的分割線------------------------------------------------ ide
對於DispatcherServlet初始化的時候初始了哪些東西,這些能夠在initStrategies中看到。 ui
/** * 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) { //初始化MultipartResolver,主要是處理文件上傳服務。 initMultipartResolver(context); //用於處理應用的國際化問題 initLocaleResolver(context); //用於定義一個主題 initThemeResolver(context); //用於定義用戶設置的請求映射關係 initHandlerMappings(context); //用於根據Handler的類型定義不一樣的處理規則 initHandlerAdapters(context); //當Handler處理錯誤的時候,經過這個handler來作統一的處理 initHandlerExceptionResolvers(context); //將指定的ViewName按照定義的RequestToViewNameTranslator替換成想要的格式。 initRequestToViewNameTranslator(context); //用於將view解析成頁面 initViewResolvers(context); //用於映射flash管理的。 initFlashMapManager(context); }--------------------------------------邪惡的分割線-------------------------------
小結: 對於spring MVC框架中,有三個組件是用戶必須定義和擴展的: this
--------------------------------------邪惡的分割線------------------------------- spa
看看DispatcherServlet啓用的時候作了哪些工做?
/** * Map config parameters onto bean properties of this servlet, and * invoke subclass initialization. * @throws ServletException if bean properties are invalid (or required * properties are missing), or if subclass initialization fails. */ @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, this.environment)); initBeanWrapper(bw); bw.setPropertyValues(pvs, true); } catch (BeansException ex) { logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex); throw ex; } // Let subclasses do whatever initialization they like. initServletBean(); if (logger.isDebugEnabled()) { logger.debug("Servlet '" + getServletName() + "' configured successfully"); } }
這樣:spring MVC的初始化工做就完成了。這樣就能夠接受你的http請求了。