SpringMVC中的ConversionServiceExposingInterceptor工具類(lèi)解析
概述
ConversionServiceExposingInterceptor是Spring MVC的一個(gè)HandlerInterceptor,用于向請(qǐng)求添加一個(gè)屬性,屬性名稱(chēng)為ConversionService.class.getName(),值是Spring MVC配置定義的一個(gè)類(lèi)型轉(zhuǎn)換服務(wù)。該類(lèi)型轉(zhuǎn)換服務(wù)會(huì)在請(qǐng)求處理過(guò)程中用于請(qǐng)求參數(shù)或者返回值的類(lèi)型轉(zhuǎn)換。
缺省情況下,Spring MVC配置機(jī)制會(huì)主動(dòng)構(gòu)建一個(gè)ConversionServiceExposingInterceptor應(yīng)用于所有的請(qǐng)求。
關(guān)于應(yīng)用
1. 引入
缺省被WebMvcConfigurationSupport啟用:
// WebMvcConfigurationSupport 代碼片段 // /** * Provide access to the shared handler interceptors used to configure * {@link HandlerMapping} instances with. * <p>This method cannot be overridden; use {@link #addInterceptors} instead. */ protected final Object[] getInterceptors() { if (this.interceptors == null) { InterceptorRegistry registry = new InterceptorRegistry(); addInterceptors(registry); registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService())); registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider())); this.interceptors = registry.getInterceptors(); } return this.interceptors.toArray(); } // requestMappingHandlerMapping() bean 定義中使用所有 getInterceptors() 定義的通用攔截器 // mapping.setInterceptors(getInterceptors()); // viewControllerHandlerMapping() bean 定義中使用所有 getInterceptors() 定義的通用攔截器 // handlerMapping.setInterceptors(getInterceptors()); // beanNameHandlerMapping() bean 定義中使用所有 getInterceptors() 定義的通用攔截器 // mapping.setInterceptors(getInterceptors()); // resourceHandlerMapping() bean 定義中使用所有 getInterceptors() 定義的通用攔截器 // handlerMapping.setInterceptors(getInterceptors());
2. 使用
DispatcherServlet#doDispatch請(qǐng)求處理主流程會(huì)先找到能處理該請(qǐng)求的Handler,以HandlerExecutionChain形式包裝存在。這些HandlerExecutionChain來(lái)自某個(gè)HandlerMapping,而這些HandlerMapping就是Spring MVC配置中定義的那些HandlerMapping,它們?cè)贒ispatcherServlet初始化階段#initHandlerMappings執(zhí)行時(shí)被從容器中獲取。
DispatcherServlet#doDispatch得到HandlerExecutionChain之后,會(huì)調(diào)用其方法applyPreHandle以應(yīng)用各個(gè)HandlerInterceptor對(duì)請(qǐng)求的前置處理邏輯。這其中就有ConversionServiceExposingInterceptor。
// HandlerExecutionChain 代碼片段 /** * Apply preHandle methods of registered interceptors. * @return {@code true} if the execution chain should proceed with the * next interceptor or the handler itself. Else, DispatcherServlet assumes * that this interceptor has already dealt with the response itself. */ boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception { HandlerInterceptor[] interceptors = getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for (int i = 0; i < interceptors.length; i++) { HandlerInterceptor interceptor = interceptors[i]; if (!interceptor.preHandle(request, response, this.handler)) { triggerAfterCompletion(request, response, null); return false; } this.interceptorIndex = i; } } return true; }
源代碼解析
源代碼版本 : spring-webmvc-5.1.5.RELEASE
package org.springframework.web.servlet.handler; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.core.convert.ConversionService; import org.springframework.util.Assert; /** * Interceptor that places the configured {@link ConversionService} in request scope * so it's available during request processing. The request attribute name is * "org.springframework.core.convert.ConversionService", the value of * {@code ConversionService.class.getName()}. * * <p>Mainly for use within JSP tags such as the spring:eval tag. * * @author Keith Donald * @since 3.0.1 */ public class ConversionServiceExposingInterceptor extends HandlerInterceptorAdapter { private final ConversionService conversionService; /** * Creates a new {@link ConversionServiceExposingInterceptor}. * @param conversionService the conversion service to export to request scope when this interceptor is invoked */ public ConversionServiceExposingInterceptor(ConversionService conversionService) { Assert.notNull(conversionService, "The ConversionService may not be null"); this.conversionService = conversionService; } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException { // 請(qǐng)求處理前向請(qǐng)求添加屬性 // 屬性名稱(chēng)使用 ConversionService.class.getName() request.setAttribute(ConversionService.class.getName(), this.conversionService); return true; } }
到此這篇關(guān)于SpringMVC中的ConversionServiceExposingInterceptor工具類(lèi)解析的文章就介紹到這了,更多相關(guān)ConversionServiceExposingInterceptor工具類(lèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用Apache工具集實(shí)現(xiàn)ftp文件傳輸代碼詳解
這篇文章主要介紹了java使用Apache工具集實(shí)現(xiàn)ftp文件傳輸代碼詳解,分享了詳細(xì)連接ftp server和上傳文件,下載文件的代碼,以及結(jié)果展示,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12Spring中的FactoryBean與BeanFactory詳細(xì)解析
這篇文章主要介紹了Spring中的FactoryBean與BeanFactory詳細(xì)解析,在Spring框架中,FactoryBean和BeanFactory是兩個(gè)關(guān)鍵的接口,用于創(chuàng)建和管理對(duì)象實(shí)例,它們?cè)赟pring的IoC(Inversion of Control,控制反轉(zhuǎn))容器中發(fā)揮著重要的作用,需要的朋友可以參考下2023-11-11org.apache.zookeeper.KeeperException.BadVersionException異常的解
在使用Apache ZooKeeper進(jìn)行分布式協(xié)調(diào)時(shí),你可能會(huì)遇到org.apache.zookeeper.KeeperException.BadVersionException異常,本文就來(lái)介紹一下解決方法,感興趣的可以了解一下2024-03-03Java使用JDK與Cglib動(dòng)態(tài)代理技術(shù)統(tǒng)一管理日志記錄
這篇文章主要介紹了Java使用JDK與Cglib動(dòng)態(tài)代理技術(shù)統(tǒng)一管理日志記錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Spring Boot實(shí)現(xiàn)微信小程序登錄
這篇文章主要為大家詳細(xì)介紹了Spring Boot實(shí)現(xiàn)微信小程序登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04jsp頁(yè)面中獲取servlet請(qǐng)求中的參數(shù)的辦法詳解
在JAVA WEB應(yīng)用中,如何獲取servlet請(qǐng)求中的參數(shù),本文講解了jsp頁(yè)面中獲取servlet請(qǐng)求中的參數(shù)的辦法2018-03-03Spring事務(wù)處理Transactional,鎖同步和并發(fā)線程
本文詳細(xì)講解了Spring事務(wù)處理Transactional,鎖同步和并發(fā)線程。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12Springboot項(xiàng)目編譯后未能加載靜態(tài)資源文件的問(wèn)題
這篇文章主要介紹了Springboot項(xiàng)目編譯后未能加載靜態(tài)資源文件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08