亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringMVC中的HandlerMappingIntrospector工具類詳解

 更新時間:2023年12月25日 10:07:05   作者:安迪源文  
這篇文章主要介紹了SpringMVC中的HandlerMappingIntrospector工具類詳解,這是一個Spring MVC助手類,用于集合應用所配置的HandlerMapping(url pattern和請求處理handler之間的映射)表,用于獲取針對某個請求的如下信息,需要的朋友可以參考下

HandlerMappingIntrospector工具類

概述

這是一個Spring MVC助手類,用于集合應用所配置的HandlerMapping(url pattern和請求處理handler之間的映射)表,用于獲取針對某個請求的如下信息 :

getMatchableHandlerMapping(javax.servlet.http.HttpServletRequest)

尋找能處理指定請求的HandlerMapping,如果找不到,返回null;如果找到的是一個MatchableHandlerMapping,則返回;如果找得到當并不是MatchableHandlerMapping,則拋出異常IllegalStateException。

getCorsConfiguration(javax.servlet.http.HttpServletRequest)

獲取針對指定請求的CORS配置,封裝為CorsConfiguration。如果不存在相應配置,返回null。

使用

作為CorsConfigurationSource使用

    // 配置類 WebMvcConfigurationSupport
	@Bean
	@Lazy
	public HandlerMappingIntrospector mvcHandlerMappingIntrospector() {
		return new HandlerMappingIntrospector();
	}

源代碼

源代碼版本 Spring Web MVC 5.1.5.RELEASE

package org.springframework.web.servlet.handler;
// 省略 imports
public class HandlerMappingIntrospector
		implements CorsConfigurationSource, ApplicationContextAware, InitializingBean {
	@Nullable
	private ApplicationContext applicationContext;
	@Nullable
	private List<HandlerMapping> handlerMappings;
	/**
	 * Constructor for use with ApplicationContextAware.
	 */
	public HandlerMappingIntrospector() {
	}
	/**
	 * Constructor that detects the configured {@code HandlerMapping}s in the
	 * given {@code ApplicationContext} or falls back on
	 * "DispatcherServlet.properties" like the {@code DispatcherServlet}.
	 * @deprecated as of 4.3.12, in favor of {@link #setApplicationContext}
	 */
	@Deprecated
	public HandlerMappingIntrospector(ApplicationContext context) {
		this.handlerMappings = initHandlerMappings(context);
	}
	/**
	 * Return the configured HandlerMapping's.
	 */
	public List<HandlerMapping> getHandlerMappings() {
		return (this.handlerMappings != null ? this.handlerMappings : Collections.emptyList());
	}
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}
   // InitializingBean 接口約定的方法,用于獲取容器中定義的所有類型為 HandlerMapping 的 bean,
   // 或者獲取缺省定義的類型為 HandlerMapping 的 bean
	@Override
	public void afterPropertiesSet() {
		if (this.handlerMappings == null) {
			Assert.notNull(this.applicationContext, "No ApplicationContext");
			this.handlerMappings = initHandlerMappings(this.applicationContext);
		}
	}
	/**
	 * Find the {@link HandlerMapping} that would handle the given request and
	 * return it as a {@link MatchableHandlerMapping} that can be used to test
	 * request-matching criteria.
	 * <p>If the matching HandlerMapping is not an instance of
	 * {@link MatchableHandlerMapping}, an IllegalStateException is raised.
	 * @param request the current request
	 * @return the resolved matcher, or {@code null}
	 * @throws Exception if any of the HandlerMapping's raise an exception
	 */
	@Nullable
	public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
		Assert.notNull(this.handlerMappings, "Handler mappings not initialized");
		HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request);
		for (HandlerMapping handlerMapping : this.handlerMappings) {
			Object handler = handlerMapping.getHandler(wrapper);
			if (handler == null) {
				continue;
			}
			if (handlerMapping instanceof MatchableHandlerMapping) {
				return ((MatchableHandlerMapping) handlerMapping);
			}
			throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping");
		}
		return null;
	}
    // 獲取某個 request 請求對應的 CorsConfiguration
    // 1. 從 handlerMappings 中找到該請求對應的 handler, 形式為 HandlerExecutionChain;
    // 2. 檢索 HandlerExecutionChain 中所有的 
	@Override
	@Nullable
	public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
		Assert.notNull(this.handlerMappings, "Handler mappings not initialized");
		HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request);
		for (HandlerMapping handlerMapping : this.handlerMappings) {
			HandlerExecutionChain handler = null;
			try {
				handler = handlerMapping.getHandler(wrapper);
			}
			catch (Exception ex) {
				// Ignore
			}
			if (handler == null) {
				continue;
			}
			if (handler.getInterceptors() != null) {
				for (HandlerInterceptor interceptor : handler.getInterceptors()) {
					if (interceptor instanceof CorsConfigurationSource) {
						return ((CorsConfigurationSource) interceptor).getCorsConfiguration(wrapper);
					}
				}
			}
			if (handler.getHandler() instanceof CorsConfigurationSource) {
				return ((CorsConfigurationSource) handler.getHandler()).getCorsConfiguration(wrapper);
			}
		}
		return null;
	}
	private static List<HandlerMapping> initHandlerMappings(ApplicationContext applicationContext) {
       // 獲取容器中所有類型為 HandlerMapping 的 bean
		Map<String, HandlerMapping> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
				applicationContext, HandlerMapping.class, true, false);
		if (!beans.isEmpty()) {
			List<HandlerMapping> mappings = new ArrayList<>(beans.values());
			AnnotationAwareOrderComparator.sort(mappings);
			return Collections.unmodifiableList(mappings);
		}
       // 容器中不存在類型為 HandlerMapping 的 bean,
       // 此時使用 initFallback 創(chuàng)建 DispatcherServlet.properties 文件中
       // 缺省定義的類型為 HandlerMapping 的 bean
		return Collections.unmodifiableList(initFallback(applicationContext));
	}
	private static List<HandlerMapping> initFallback(ApplicationContext applicationContext) {
		Properties props;
		String path = "DispatcherServlet.properties";
		try {
			Resource resource = new ClassPathResource(path, DispatcherServlet.class);
			props = PropertiesLoaderUtils.loadProperties(resource);
		}
		catch (IOException ex) {
			throw new IllegalStateException("Could not load '" + path + "': " + ex.getMessage());
		}
		String value = props.getProperty(HandlerMapping.class.getName());
		String[] names = StringUtils.commaDelimitedListToStringArray(value);
		List<HandlerMapping> result = new ArrayList<>(names.length);
		for (String name : names) {
			try {
				Class<?> clazz = ClassUtils.forName(name, DispatcherServlet.class.getClassLoader());
              // 使用容器創(chuàng)建 bean,該動作會使所創(chuàng)建的 bean 經歷相應的生命周期處理,比如初始化,屬性設置等等  
				Object mapping = applicationContext.getAutowireCapableBeanFactory().createBean(clazz);
				result.add((HandlerMapping) mapping);
			}
			catch (ClassNotFoundException ex) {
				throw new IllegalStateException("Could not find default HandlerMapping [" + name + "]");
			}
		}
		return result;
	}
	/**
	 * Request wrapper that ignores request attribute changes.
     * 定義一個給當前 HandlerMappingIntrospector 使用的 HttpServletRequestWrapper,
     * 其方法 setAttribute 實現為空,主要是用于避免屬性修改,也就是保持對所訪問的 request 不做修改
	 */
	private static class RequestAttributeChangeIgnoringWrapper extends HttpServletRequestWrapper {
		public RequestAttributeChangeIgnoringWrapper(HttpServletRequest request) {
			super(request);
		}
		@Override
		public void setAttribute(String name, Object value) {
			// Ignore attribute change...
		}
	}
}

到此這篇關于SpringMVC中的HandlerMappingIntrospector工具類詳解的文章就介紹到這了,更多相關HandlerMappingIntrospector工具類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 解決IDEA集成Docker插件后出現日志亂碼的問題

    解決IDEA集成Docker插件后出現日志亂碼的問題

    這篇文章主要介紹了解決IDEA集成Docker插件后出現日志亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • SpringBoot 多環(huán)境配置和啟動詳解

    SpringBoot 多環(huán)境配置和啟動詳解

    這篇文章主要為大家介紹了SpringBoot多環(huán)境配置和啟動詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • 詳解JVM系列之對象的鎖狀態(tài)和同步

    詳解JVM系列之對象的鎖狀態(tài)和同步

    鎖和同步是java多線程編程中非常常見的使用場景。為了鎖定多線程共享的對象,Java需要提供一定的機制來實現共享對象的鎖定。當第二個線程進入同一個區(qū)域的時候,必須等待第一個線程解鎖該對象。JVM是怎么做到的呢?快來一起看看吧。
    2021-06-06
  • Java中監(jiān)聽器Listener詳解

    Java中監(jiān)聽器Listener詳解

    Listener是由Java編寫的WEB組件,主要完成對內置對象狀態(tài)的變化 (創(chuàng)建、銷毀)和屬性的變化進行監(jiān)聽,做進一步的處理,主要對session和application內置對象監(jiān)聽,這篇文章主要介紹了Java中監(jiān)聽器Listener,需要的朋友可以參考下
    2023-08-08
  • 前端RSA加密java后端解密示例代碼

    前端RSA加密java后端解密示例代碼

    這篇文章主要介紹了RSA非對稱加密的原理,前端使用公鑰加密數據,后端使用私鑰解密,提供了前端和后端實現的示例代碼,包括依賴、接口、工具類等,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-11-11
  • Java徹底消滅if-else的8種方案

    Java徹底消滅if-else的8種方案

    這篇文章主要給大家介紹了關于Java徹底消滅if-else的8種方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Java裁剪壓縮PNG圖片,透明背景色變黑的解決方案

    Java裁剪壓縮PNG圖片,透明背景色變黑的解決方案

    這篇文章主要介紹了Java裁剪壓縮PNG圖片,透明背景色變黑的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java 多線程并發(fā)編程_動力節(jié)點Java學院整理

    Java 多線程并發(fā)編程_動力節(jié)點Java學院整理

    這篇文章主要介紹了Java 多線程并發(fā)編程的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-05-05
  • Debian 7 和 Debian 8 用戶安裝 Java 8的方法

    Debian 7 和 Debian 8 用戶安裝 Java 8的方法

    Oracle Java 8 穩(wěn)定版本近期已發(fā)布,有很多新的特征變化。其中,有功能的程序支持通過“Lambda項目 ”,收到了一些安全更新和界面改進上的bug修復,使得開發(fā)人員的工作更容易。
    2014-03-03
  • zookeeper服務優(yōu)化的一些建議

    zookeeper服務優(yōu)化的一些建議

    今天小編就為大家分享一篇關于zookeeper服務優(yōu)化的一些建議,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03

最新評論