最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中PathMatcher路徑匹配器的實現(xiàn)

 更新時間:2024年07月11日 11:31:33   作者:濤哥是個大帥比  
Spring框架中的PathMatcher是一個接口,本文主要介紹了Spring中PathMatcher路徑匹配器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

概述:

PathMatcher是Spring的一個概念模型接口,該接口抽象建模了概念"路徑匹配器",一個"路徑匹配器"是一個用于路徑匹配的工具。位于 Spring-core 包中 util 包下。

PathMatcher 接口源碼

package org.springframework.util;
 
import java.util.Comparator;
import java.util.Map;
 
public interface PathMatcher {
 
    /**
     * Does the given path represent a pattern that can be matched
     * by an implementation of this interface?
     * 判斷指定的路徑 path 是否是一個 pattern(模式)
     * 如果返回值是 false,也就是說 path 不是一個模式,而是一個靜態(tài)路徑(真正的路徑字符串),
     * 那么就不用調(diào)用方法 #match 了,因為對于靜態(tài)路徑的匹配,直接使用字符串等號比較就足夠了。
     * @param path the path String to check
     * @return true if the given path represents a pattern
     */
    boolean isPattern(String path);
 
    /**
     * Match the given path against the given pattern,
     * according to this PathMatcher's matching strategy.
     * 根據(jù)當(dāng)前 PathMatcher 的匹配策略,檢查指定的徑 path 和指定的模式 pattern 是否匹配
     * @param 用于檢測路徑字符串是否匹配于某個模式時所用的模式
     * @param path 需要被檢測的路徑字符串
     * @return true 表示匹配, false 表示不匹配
     */
    boolean match(String pattern, String path);
 
    /**
     * Match the given  path against the corresponding part of the given
     * pattern, according to this PathMatcher's matching strategy.
     * 根據(jù)當(dāng)前 PathMatcher 的匹配策略,檢查指定的徑 path 和指定的模式 pattern 是否之間
     * 是否為前綴匹配
     * @param pattern the pattern to match against
     * @param path the path String to test
     * @return true 表示匹配, false 表示不匹配
     */
    boolean matchStart(String pattern, String path);
 
    /**
     * Given a pattern and a full path, determine the pattern-mapped part.
     * 給定一個模式 pattern 和一個全路徑 path,判斷路徑中和模式匹配的部分。
     *
     * This method is supposed to find out which part of the path is matched
     * dynamically through an actual pattern, that is, it strips off a statically
     * defined leading path from the given full path, returning only the actually
     * pattern-matched part of the path.
     * 該方法用于發(fā)現(xiàn)路徑中的哪一部分是和模式能動態(tài)匹配上的部分。它會去除路徑中開頭靜態(tài)部分,
     * 僅僅返回那部分真正和模式匹配的上的部分。
     * 例子 : "myroot/*.html" 為 pattern , "myroot/myfile.html" 為路徑,
     *  則該方法返回 "myfile.html".  
     * 具體的檢測規(guī)則根據(jù)當(dāng)前 PathMatcher 的匹配策略來頂。    
     * A simple implementation may return the given full path as-is in case
     * of an actual pattern, and the empty String in case of the pattern not
     * containing any dynamic parts (i.e. the pattern parameter being
     * a static path that wouldn't qualify as an actual #isPattern pattern.
     * A sophisticated implementation will differentiate between the static parts
     * and the dynamic parts of the given path pattern.
     * @param pattern the path pattern
     * @param path the full path to introspect
     * @return the pattern-mapped part of the given path
     * (never null)
     */
    String extractPathWithinPattern(String pattern, String path);
 
    /**
     * Given a pattern and a full path, extract the URI template variables. URI template
     * variables are expressed through curly brackets ('{' and '}').
     * 給定一個模式和一個路徑,提取其中的 URI 模板變量信息。URI模板變量表達式格式為 "{variable}"
     *      
     * 例子 : pattern  為 "/hotels/{hotel}" ,路徑為 "/hotels/1", 則該方法會返回一個 map ,
     * 內(nèi)容為 : "hotel"->"1".
     * @param pattern the path pattern, possibly containing URI templates
     * @param path the full path to extract template variables from
     * @return a map, containing variable names as keys; variables values as values
     */
    Map<String, String> extractUriTemplateVariables(String pattern, String path);
 
    /**
     * Given a full path, returns a Comparator suitable for sorting patterns
     * in order of explicitness for that path.
     * The full algorithm used depends on the underlying implementation,
     * but generally, the returned Comparator will sort a list so that more 
     * specific patterns come before generic patterns.
     * @param path the full path to use for comparison
     * @return a comparator capable of sorting patterns in order of explicitness
     */
    Comparator<String> getPatternComparator(String path);
 
    /**
     * Combines two patterns into a new pattern that is returned.
     * The full algorithm used for combining the two pattern depends on the underlying implementation.
     * 合并兩個模式。具體合并的算法由實現(xiàn)類決定。
     * @param pattern1 the first pattern
     * @param pattern2 the second pattern
     * @return the combination of the two patterns
     * @throws IllegalArgumentException when the two patterns cannot be combined
     */
    String combine(String pattern1, String pattern2);
 
}

AntPathMatcher:

Spring框架自身對概念模型接口也提供了一個缺省的實現(xiàn)AntPathMatcher,用于匹配Ant風(fēng)格的路徑。我們平時使用的路徑匹配基本用的都是這個。

1.通配符:

? 表示匹配任意一個字符

System.out.println( matcher.match("/usr/name?", "/usr/name1") );  //true
System.out.println( matcher.match("/usr/name?", "/usr/name111") );  //false

* 表示匹配單個路徑

System.out.println( matcher.match("/usr/*", "/usr/list")); //true
System.out.println( matcher.match("/usr/*", "/usr/list/again")); //false
System.out.println( matcher.match("/usr/*.app", "/usr/list.app")); //true
System.out.println( matcher.match("/usr/*.app", "/usr/list.html")); //false

 ** 表示匹配多級路徑

System.out.println( matcher.match("/usr/**", "/usr/list") );  //true
System.out.println( matcher.match("/usr/**", "/usr/list/again") );  //true

變量綁定匹配

System.out.println( matcher.match("/usr/{name}/{age}", "/usr/張三/12") );  //true
System.out.println( matcher.match("/usr/{name}/{age}", "/usr/張三") );  //false

 通過 extractPathWithinPattern() 方法匹配

/**
 * extractPathWithinPattern 返回符合匹配的部分路徑,去除路徑開頭靜態(tài)部分
 **/
System.out.println( matcher.extractPathWithinPattern("/usr/*", "/usr/list") );   //list
System.out.println( matcher.extractPathWithinPattern("/usr/*/again", "/usr/list/again/age") );   // list/again/age
System.out.println( matcher.extractPathWithinPattern("/usr/*.html", "/usr/list") );  //list
System.out.println( matcher.extractPathWithinPattern("/usr/*.html", "/usr/list.html") );  //list.html

2.變量綁定

變量綁定可以讓我們在匹配過程中提取路徑中的參數(shù),并將其賦值給指定的變量。

System.out.println( matcher.extractUriTemplateVariables("/usr/{name}/{age}", "/usr/張三/12") ); //{name=張三, age=12}

到此這篇關(guān)于Spring中PathMatcher路徑匹配器的實現(xiàn)的文章就介紹到這了,更多相關(guān)Spring PathMatcher路徑匹配器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • SpringBoot 異步線程間數(shù)據(jù)傳遞的實現(xiàn)

    SpringBoot 異步線程間數(shù)據(jù)傳遞的實現(xiàn)

    本文主要介紹了SpringBoot 異步線程間數(shù)據(jù)傳遞的實現(xiàn),包括異步線程的基本概念、數(shù)據(jù)傳遞的方式、具體實現(xiàn)方式等,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • SpringBoot基礎(chǔ)教程之集成郵件服務(wù)

    SpringBoot基礎(chǔ)教程之集成郵件服務(wù)

    這篇文章主要給大家介紹了關(guān)于SpringBoot基礎(chǔ)教程之集成郵件服務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者使用SpringBoot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Springboot整合Dubbo+Nacos實現(xiàn)RPC調(diào)用的示例代碼

    Springboot整合Dubbo+Nacos實現(xiàn)RPC調(diào)用的示例代碼

    隨著互聯(lián)網(wǎng)技術(shù)的飛速發(fā)展,越來越多的企業(yè)和開發(fā)者開始關(guān)注微服務(wù)架構(gòu),Nacos是阿里巴巴開源的一個動態(tài)服務(wù)發(fā)現(xiàn)、配置管理和服務(wù)管理平臺,本文講解如何將Spring Boot與Dubbo和Nacos整合,實現(xiàn)RPC調(diào)用,需要的朋友可以參考下
    2024-02-02
  • MyBatis?與?Spring?Data?JPA?核心對比總結(jié)(選型指南與最佳實踐)

    MyBatis?與?Spring?Data?JPA?核心對比總結(jié)(選型指南與最佳實踐)

    MyBatis和Spring?Data?JPA是Java兩大主流持久層框架,分別代表SQL可控性與面向?qū)ο箝_發(fā)效率的兩種設(shè)計哲學(xué),本文給大家介紹MyBatis與Spring?Data?JPA核心對比總結(jié),感興趣的朋友跟隨小編一起看看吧
    2025-11-11
  • Spring之@Lookup注解詳細解析

    Spring之@Lookup注解詳細解析

    這篇文章主要介紹了Spring之@Lookup注解詳細解析,當(dāng)采用@Autowired注解對單例bean注依賴的原型bean時,會由于單例bean只會創(chuàng)建一次,導(dǎo)致依賴的原型bean也只會注入一次,@Lookup注解可以較為優(yōu)雅的解決此類問題,需要的朋友可以參考下
    2024-01-01
  • 關(guān)于SpringMVC的異常處理機制詳細解讀

    關(guān)于SpringMVC的異常處理機制詳細解讀

    這篇文章主要介紹了關(guān)于SpringMVC的異常處理機制詳細解讀,SpringMVC是目前主流的Web?MVC框架之一,本文將分析SpringMVC的異常處理內(nèi)容,需要的朋友可以參考下
    2023-05-05
  • Java之注解@Data和@ToString(callSuper=true)解讀

    Java之注解@Data和@ToString(callSuper=true)解讀

    在使用Lombok庫的@Data注解時,若子類未通過@ToString(callSuper=true)注明包含父類屬性,toString()方法只打印子類屬性,解決方法:1. 子類重寫toString方法;2. 子類使用@Data和@ToString(callSuper=true),父類也應(yīng)使用@Data
    2024-11-11
  • IDEA生成javadoc的實現(xiàn)步驟

    IDEA生成javadoc的實現(xiàn)步驟

    Javadoc是一種用于生成API文檔的工具,它可以根據(jù)代碼中特定格式的注釋自動生成文檔,本文主要介紹了IDEA生成javadoc的實現(xiàn)步驟,感興趣的可以了解一下
    2023-10-10
  • java鎖機制ReentrantLock源碼實例分析

    java鎖機制ReentrantLock源碼實例分析

    這篇文章主要為大家介紹了java鎖機制ReentrantLock源碼實例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • SpringBoot整合WxJava開啟消息推送的實現(xiàn)

    SpringBoot整合WxJava開啟消息推送的實現(xiàn)

    本文主要介紹了SpringBoot整合WxJava開啟消息推送,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03

最新評論

洱源县| 常州市| 天长市| 赣州市| 洛川县| 武功县| 贺州市| 泰兴市| 漾濞| 吉林省| 华坪县| 金平| 确山县| 陆良县| 柳州市| 阳高县| 崇仁县| 岑巩县| 淮滨县| 大荔县| 共和县| 霍林郭勒市| 鄯善县| 渑池县| 东阿县| 四平市| 台前县| 武冈市| 凭祥市| 灵璧县| 满洲里市| 海安县| 新和县| 舒城县| 本溪市| 临沧市| 友谊县| 墨脱县| 乌兰察布市| 偏关县| 睢宁县|