日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring3: 4.4 使用路径通配符加载Resource

發布時間:2024/9/5 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring3: 4.4 使用路径通配符加载Resource 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

4.4.1? 使用路徑通配符加載Resource

?????? 前面介紹的資源路徑都是非常簡單的一個路徑匹配一個資源,Spring還提供了一種更強大的Ant模式通配符匹配,從能一個路徑匹配一批資源。

?

?????? Ant路徑通配符支持“?”、“*”、“**”,注意通配符匹配不包括目錄分隔符“/”:

?

? ? ? ? ?“?”:匹配一個字符,如“config?.xml”將匹配“config1.xml”;

? ? ? ? ?“*”:匹配零個或多個字符串,如“cn/*/config.xml”將匹配“cn/javass/config.xml”,但不匹配匹配“cn/config.xml”;而“cn/config-*.xml”將匹配“cn/config-dao.xml”;

? ? ? ? ?“**”:匹配路徑中的零個或多個目錄,如“cn/**/config.xml”將匹配“cn /config.xml”,也匹配“cn/javass/spring/config.xml”;而“cn/javass/config-**.xml”將匹配“cn/javass/config-dao.xml”,即把“**”當做兩個“*”處理。

?

Spring提供AntPathMatcher來進行Ant風格的路徑匹配。具體測試請參考cn.javass.spring.chapter4. AntPathMatcherTest。

public class AntPathMatcherTest {private PathMatcher pathMatcher = new AntPathMatcher();@Testpublic void testQuestionMark() {Assert.assertTrue(pathMatcher.match("config?.xml", "config1.xml"));Assert.assertFalse(pathMatcher.match("config?.xml", "config12.xml"));Assert.assertFalse(pathMatcher.match("config?.xml", "config.xml"));}@Testpublic void testOneAsterisk() {Assert.assertTrue(pathMatcher.match("config-*.xml", "config-dao.xml"));Assert.assertTrue(pathMatcher.match("config-*.xml", "config-.xml"));Assert.assertTrue(pathMatcher.match("config-**.xml", "config-dao.xml"));Assert.assertFalse(pathMatcher.match("config-*.xml", "config-1/.xml"));Assert.assertFalse(pathMatcher.match("config-*.xml", "config-1/2.xml"));Assert.assertTrue(pathMatcher.match("/cn/*/config.xml", "/cn/javass/config.xml"));Assert.assertFalse(pathMatcher.match("/cn/*/config.xml", "/cn/config.xml"));Assert.assertFalse(pathMatcher.match("/cn/*/config.xml", "/cn//config.xml"));Assert.assertFalse(pathMatcher.match("/cn/*/config.xml", "/cn/javass/spring/config.xml"));}@Testpublic void testTwoAsterisk() {Assert.assertTrue(pathMatcher.match("/cn/**/config-*.xml", "/cn/javass/config-dao.xml"));Assert.assertTrue(pathMatcher.match("/cn/**/config-*.xml", "/cn/javass/spring/config-dao.xml"));Assert.assertTrue(pathMatcher.match("/cn/**/config-*.xml", "/cn/config-dao.xml"));} }

  

?

?

?

Spring在加載類路徑資源時除了提供前綴“classpath:”的來支持加載一個Resource,還提供一個前綴“classpath*:”來支持加載所有匹配的類路徑Resource。

?

Spring提供ResourcePatternResolver接口來加載多個Resource,該接口繼承了ResourceLoader并添加了“Resource[] getResources(String locationPattern)”用來加載多個Resource:

public interface ResourcePatternResolver extends ResourceLoader { String CLASSPATH_ALL_URL_PREFIX = "classpath*:"; Resource[] getResources(String locationPattern) throws IOException; }

  

Spring提供了一個ResourcePatternResolver實現PathMatchingResourcePatternResolver,它是基于模式匹配的,默認使用AntPathMatcher進行路徑匹配,它除了支持ResourceLoader支持的前綴外,還額外支持“classpath*:”用于加載所有匹配的類路徑Resource,ResourceLoader不支持前綴“classpath*:”:

?

首先做下準備工作,在項目的“resources”創建“META-INF”目錄,然后在其下創建一個“INDEX.LIST”文件。同時在“org.springframework.beans-3.0.5.RELEASE.jar”和“org.springframework.context-3.0.5.RELEASE.jar”兩個jar包里也存在相同目錄和文件。然后創建一個“LICENSE”文件,該文件存在于“com.springsource.cn.sf.cglib-2.2.0.jar”里。

?

?

一、“classpath”:?用于加載類路徑(包括jar包)中的一個且僅一個資源;對于多個匹配的也只返回一個,所以如果需要多個匹配的請考慮“classpath*:”前綴;

@Test public void testClasspathPrefix() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //只加載一個絕對匹配Resource,且通過ResourceLoader.getResource進行加載 Resource[] resources=resolver.getResources("classpath:META-INF/INDEX.LIST"); Assert.assertEquals(1, resources.length); //只加載一個匹配的Resource,且通過ResourceLoader.getResource進行加載 resources = resolver.getResources("classpath:META-INF/*.LIST"); Assert.assertTrue(resources.length == 1); }

  二、“classpath*”:?用于加載類路徑(包括jar包)中的所有匹配的資源。帶通配符的classpath使用“ClassLoader”的“Enumeration<URL>?getResources(String?name)”方法來查找通配符之前的資源,然后通過模式匹配來獲取匹配的資源。如“classpath:META-INF/*.LIST”將首先加載通配符之前的目錄“META-INF”,然后再遍歷路徑進行子路徑匹配從而獲取匹配的資源。

@Test public void testClasspathAsteriskPrefix () throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //將加載多個絕對匹配的所有Resource //將首先通過ClassLoader.getResources("META-INF")加載非模式路徑部分 //然后進行遍歷模式匹配 Resource[] resources=resolver.getResources("classpath*:META-INF/INDEX.LIST"); Assert.assertTrue(resources.length > 1); //將加載多個模式匹配的Resource resources = resolver.getResources("classpath*:META-INF/*.LIST"); Assert.assertTrue(resources.length > 1); }

  

注意“resources.length >1”說明返回多個Resource。不管模式匹配還是非模式匹配只要匹配的都將返回。

?

?????? 在“com.springsource.cn.sf.cglib-2.2.0.jar”里包含“asm-license.txt”文件,對于使用“classpath*: asm-*.txt”進行通配符方式加載資源將什么也加載不了“asm-license.txt”文件,注意一定是模式路徑匹配才會遇到這種問題。這是由于“ClassLoader”的“getResources(String?name)”方法的限制,對于name為“”的情況將只返回文件系統的類路徑,不會包換jar包根路徑。

@Test public void testClasspathAsteriskPrefixLimit() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //將首先通過ClassLoader.getResources("")加載目錄, //將只返回文件系統的類路徑不返回jar的跟路徑 //然后進行遍歷模式匹配 Resource[] resources = resolver.getResources("classpath*:asm-*.txt"); Assert.assertTrue(resources.length == 0); //將通過ClassLoader.getResources("asm-license.txt")加載 //asm-license.txt存在于com.springsource.net.sf.cglib-2.2.0.jar resources = resolver.getResources("classpath*:asm-license.txt"); Assert.assertTrue(resources.length > 0); //將只加載文件系統類路徑匹配的Resource resources = resolver.getResources("classpath*:LICENS*"); Assert.assertTrue(resources.length == 1); }

  

對于“resolver.getResources("classpath*:asm-*.txt");”,由于在項目“resources”目錄下沒有所以應該返回0個資源;“resolver.getResources("classpath*:asm-license.txt");”將返回jar包里的Resource;“resolver.getResources("classpath*:LICENS*");”,因為將只返回文件系統類路徑資源,所以返回1個資源。

?

因此加載通配符路徑時(即路徑中包含通配符),必須包含一個根目錄才能保證加載的資源是所有的,而不是部分。

?

?

三、“file”:加載一個或多個文件系統中的Resource。如“file:D:/*.txt”將返回D盤下的所有txt文件;??????

?

四、無前綴:通過ResourceLoader實現加載一個資源。

?

AppliacationContext提供的getResources方法將獲取資源委托給ResourcePatternResolver實現,默認使用PathMatchingResourcePatternResolver。所有在此就無需介紹其使用方法了。

?

4.4.2? 注入Resource數組

?????? Spring還支持注入Resource數組,直接看配置如下:

<bean id="resourceBean1" class="cn.javass.spring.chapter4.bean.ResourceBean4"> <property name="resources"> <array> <value>cn/javass/spring/chapter4/test1.properties</value> <value>log4j.xml</value> </array> </property> </bean> <bean id="resourceBean2" class="cn.javass.spring.chapter4.bean.ResourceBean4"> <property name="resources" value="classpath*:META-INF/INDEX.LIST"/> </bean> <bean id="resourceBean3" class="cn.javass.spring.chapter4.bean.ResourceBean4"> <property name="resources"> <array> <value>cn/javass/spring/chapter4/test1.properties</value> <value>classpath*:META-INF/INDEX.LIST</value> </array> </property> </bean>

  

“resourceBean1”就不用多介紹了,傳統實現方式;對于“resourceBean2”則使用前綴“classpath*”,看到這大家應該懂的,加載匹配多個資源;“resourceBean3”是混合使用的;測試代碼在“cn.javass.spring.chapter4.ResourceInjectTest.testResourceArrayInject”。

?????? Spring通過ResourceArrayPropertyEditor來進行類型轉換的,而它又默認使用“PathMatchingResourcePatternResolver”來進行把路徑解析為Resource對象。所有大家只要會使用“PathMatchingResourcePatternResolver”,其它一些實現都是委托給它的,比如AppliacationContext的“getResources”方法等。

?

4.4.3? AppliacationContext實現對各種Resource的支持

???????一、ClassPathXmlApplicationContext默認將通過classpath進行加載返回ClassPathResource,提供兩類構造器方法:

public class ClassPathXmlApplicationContext { //1)通過ResourcePatternResolver實現根據configLocation獲取資源 public ClassPathXmlApplicationContext(String configLocation); public ClassPathXmlApplicationContext(String... configLocations); public ClassPathXmlApplicationContext(String[] configLocations, ……); //2)通過直接根據path直接返回ClasspathResource public ClassPathXmlApplicationContext(String path, Class clazz); public ClassPathXmlApplicationContext(String[] paths, Class clazz); public ClassPathXmlApplicationContext(String[] paths, Class clazz, ……); }

  

第一類構造器是根據提供的配置文件路徑使用“ResourcePatternResolver ”的“getResources()”接口通過匹配獲取資源;即如“classpath:config.xml”

?????? 第二類構造器則是根據提供的路徑和clazz來構造ClassResource資源。即采用“public ClassPathResource(String path, Class<?> clazz)”構造器獲取資源。

?

?

???????二、FileSystemXmlApplicationContext將加載相對于當前工作目錄的“configLocation”位置的資源,注意在linux系統上不管“configLocation”是否帶“/”,都作為相對路徑;而在window系統上如“D:/resourceInject.xml”是絕對路徑。因此在除非很必要的情況下,不建議使用該ApplicationContext。

public class FileSystemXmlApplicationContext{ public FileSystemXmlApplicationContext(String configLocation); public FileSystemXmlApplicationContext(String... configLocations,……); }

  

//linux系統,以下全是相對于當前vm路徑進行加載 new FileSystemXmlApplicationContext("chapter4/config.xml"); new FileSystemXmlApplicationContext("/chapter4/confg.xml");

  

//windows系統,第一個將相對于當前vm路徑進行加載; //第二個則是絕對路徑方式加載 new FileSystemXmlApplicationContext("chapter4/config.xml"); new FileSystemXmlApplicationContext("d:/chapter4/confg.xml");

  ?此處還需要注意:在linux系統上,構造器使用的是相對路徑,而ctx.getResource()方法如果以“/”開頭則表示獲取絕對路徑資源,而不帶前導“/”將返回相對路徑資源。如下:

//linux系統,第一個將相對于當前vm路徑進行加載; //第二個則是絕對路徑方式加載 ctx.getResource ("chapter4/config.xml"); ctx.getResource ("/root/confg.xml"); //windows系統,第一個將相對于當前vm路徑進行加載; //第二個則是絕對路徑方式加載 ctx.getResource ("chapter4/config.xml"); ctx.getResource ("d:/chapter4/confg.xml");

  ?因此如果需要加載絕對路徑資源最好選擇前綴“file”方式,將全部根據絕對路徑加載。如在linux系統“ctx.getResource ("file:/root/confg.xml");”? ??

?

轉載于:https://www.cnblogs.com/achengmu/p/8440780.html

總結

以上是生活随笔為你收集整理的spring3: 4.4 使用路径通配符加载Resource的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。