SpringMVC中的ConversionServiceExposingInterceptor工具類解析
概述
ConversionServiceExposingInterceptor是Spring MVC的一個HandlerInterceptor,用于向請求添加一個屬性,屬性名稱為ConversionService.class.getName(),值是Spring MVC配置定義的一個類型轉(zhuǎn)換服務(wù)。該類型轉(zhuǎn)換服務(wù)會在請求處理過程中用于請求參數(shù)或者返回值的類型轉(zhuǎn)換。
缺省情況下,Spring MVC配置機制會主動構(gòu)建一個ConversionServiceExposingInterceptor應(yī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請求處理主流程會先找到能處理該請求的Handler,以HandlerExecutionChain形式包裝存在。這些HandlerExecutionChain來自某個HandlerMapping,而這些HandlerMapping就是Spring MVC配置中定義的那些HandlerMapping,它們在DispatcherServlet初始化階段#initHandlerMappings執(zhí)行時被從容器中獲取。
DispatcherServlet#doDispatch得到HandlerExecutionChain之后,會調(diào)用其方法applyPreHandle以應(yīng)用各個HandlerInterceptor對請求的前置處理邏輯。這其中就有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 {
// 請求處理前向請求添加屬性
// 屬性名稱使用 ConversionService.class.getName()
request.setAttribute(ConversionService.class.getName(), this.conversionService);
return true;
}
}到此這篇關(guān)于SpringMVC中的ConversionServiceExposingInterceptor工具類解析的文章就介紹到這了,更多相關(guān)ConversionServiceExposingInterceptor工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java將網(wǎng)絡(luò)圖片轉(zhuǎn)成輸入流以及將url轉(zhuǎn)成InputStream問題
這篇文章主要介紹了Java將網(wǎng)絡(luò)圖片轉(zhuǎn)成輸入流以及將url轉(zhuǎn)成InputStream問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Java 垃圾回收機制詳解(動力節(jié)點Java學(xué)院整理)
在系統(tǒng)運行過程中,會產(chǎn)生一些無用的對象,這些對象占據(jù)著一定的內(nèi)存,如果不對這些對象清理回收無用對象的內(nèi)存,可能會導(dǎo)致內(nèi)存的耗盡,所以垃圾回收機制回收的是內(nèi)存。下面通過本文給大家詳細(xì)介紹java垃圾回收機制,一起學(xué)習(xí)吧2017-02-02
Java如何取掉json數(shù)據(jù)中值為null的屬性字段
這篇文章主要介紹了Java如何取掉json數(shù)據(jù)中值為null的屬性字段,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實例詳解
在本篇文章里小編給大家分享的是關(guān)于java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-05-05

