spring3: 4.4 使用路径通配符加载Resource
4.4.1? 使用路徑通配符加載Resource
?????? 前面介紹的資源路徑都是非常簡單的一個(gè)路徑匹配一個(gè)資源,Spring還提供了一種更強(qiáng)大的Ant模式通配符匹配,從能一個(gè)路徑匹配一批資源。
?
?????? Ant路徑通配符支持“?”、“*”、“**”,注意通配符匹配不包括目錄分隔符“/”:
?
? ? ? ? ?“?”:匹配一個(gè)字符,如“config?.xml”將匹配“config1.xml”;
? ? ? ? ?“*”:匹配零個(gè)或多個(gè)字符串,如“cn/*/config.xml”將匹配“cn/javass/config.xml”,但不匹配匹配“cn/config.xml”;而“cn/config-*.xml”將匹配“cn/config-dao.xml”;
? ? ? ? ?“**”:匹配路徑中的零個(gè)或多個(gè)目錄,如“cn/**/config.xml”將匹配“cn /config.xml”,也匹配“cn/javass/spring/config.xml”;而“cn/javass/config-**.xml”將匹配“cn/javass/config-dao.xml”,即把“**”當(dāng)做兩個(gè)“*”處理。
?
Spring提供AntPathMatcher來進(jìn)行Ant風(fēng)格的路徑匹配。具體測試請參考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在加載類路徑資源時(shí)除了提供前綴“classpath:”的來支持加載一個(gè)Resource,還提供一個(gè)前綴“classpath*:”來支持加載所有匹配的類路徑Resource。
?
Spring提供ResourcePatternResolver接口來加載多個(gè)Resource,該接口繼承了ResourceLoader并添加了“Resource[] getResources(String locationPattern)”用來加載多個(gè)Resource:
public interface ResourcePatternResolver extends ResourceLoader { String CLASSPATH_ALL_URL_PREFIX = "classpath*:"; Resource[] getResources(String locationPattern) throws IOException; }
Spring提供了一個(gè)ResourcePatternResolver實(shí)現(xiàn)PathMatchingResourcePatternResolver,它是基于模式匹配的,默認(rèn)使用AntPathMatcher進(jìn)行路徑匹配,它除了支持ResourceLoader支持的前綴外,還額外支持“classpath*:”用于加載所有匹配的類路徑Resource,ResourceLoader不支持前綴“classpath*:”:
?
首先做下準(zhǔn)備工作,在項(xiàng)目的“resources”創(chuàng)建“META-INF”目錄,然后在其下創(chuàng)建一個(gè)“INDEX.LIST”文件。同時(shí)在“org.springframework.beans-3.0.5.RELEASE.jar”和“org.springframework.context-3.0.5.RELEASE.jar”兩個(gè)jar包里也存在相同目錄和文件。然后創(chuàng)建一個(gè)“LICENSE”文件,該文件存在于“com.springsource.cn.sf.cglib-2.2.0.jar”里。
?
?
一、“classpath”:?用于加載類路徑(包括jar包)中的一個(gè)且僅一個(gè)資源;對于多個(gè)匹配的也只返回一個(gè),所以如果需要多個(gè)匹配的請考慮“classpath*:”前綴;
@Test public void testClasspathPrefix() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //只加載一個(gè)絕對匹配Resource,且通過ResourceLoader.getResource進(jìn)行加載 Resource[] resources=resolver.getResources("classpath:META-INF/INDEX.LIST"); Assert.assertEquals(1, resources.length); //只加載一個(gè)匹配的Resource,且通過ResourceLoader.getResource進(jìn)行加載 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”,然后再遍歷路徑進(jìn)行子路徑匹配從而獲取匹配的資源。
@Test public void testClasspathAsteriskPrefix () throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //將加載多個(gè)絕對匹配的所有Resource //將首先通過ClassLoader.getResources("META-INF")加載非模式路徑部分 //然后進(jìn)行遍歷模式匹配 Resource[] resources=resolver.getResources("classpath*:META-INF/INDEX.LIST"); Assert.assertTrue(resources.length > 1); //將加載多個(gè)模式匹配的Resource resources = resolver.getResources("classpath*:META-INF/*.LIST"); Assert.assertTrue(resources.length > 1); }
注意“resources.length >1”說明返回多個(gè)Resource。不管模式匹配還是非模式匹配只要匹配的都將返回。
?
?????? 在“com.springsource.cn.sf.cglib-2.2.0.jar”里包含“asm-license.txt”文件,對于使用“classpath*: asm-*.txt”進(jìn)行通配符方式加載資源將什么也加載不了“asm-license.txt”文件,注意一定是模式路徑匹配才會(huì)遇到這種問題。這是由于“ClassLoader”的“getResources(String?name)”方法的限制,對于name為“”的情況將只返回文件系統(tǒng)的類路徑,不會(huì)包換jar包根路徑。
@Test public void testClasspathAsteriskPrefixLimit() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //將首先通過ClassLoader.getResources("")加載目錄, //將只返回文件系統(tǒng)的類路徑不返回jar的跟路徑 //然后進(jìn)行遍歷模式匹配 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); //將只加載文件系統(tǒng)類路徑匹配的Resource resources = resolver.getResources("classpath*:LICENS*"); Assert.assertTrue(resources.length == 1); }
對于“resolver.getResources("classpath*:asm-*.txt");”,由于在項(xiàng)目“resources”目錄下沒有所以應(yīng)該返回0個(gè)資源;“resolver.getResources("classpath*:asm-license.txt");”將返回jar包里的Resource;“resolver.getResources("classpath*:LICENS*");”,因?yàn)閷⒅环祷匚募到y(tǒng)類路徑資源,所以返回1個(gè)資源。
?
因此加載通配符路徑時(shí)(即路徑中包含通配符),必須包含一個(gè)根目錄才能保證加載的資源是所有的,而不是部分。
?
?
三、“file”:加載一個(gè)或多個(gè)文件系統(tǒng)中的Resource。如“file:D:/*.txt”將返回D盤下的所有txt文件;??????
?
四、無前綴:通過ResourceLoader實(shí)現(xiàn)加載一個(gè)資源。
?
AppliacationContext提供的getResources方法將獲取資源委托給ResourcePatternResolver實(shí)現(xiàn),默認(rèn)使用PathMatchingResourcePatternResolver。所有在此就無需介紹其使用方法了。
?
4.4.2? 注入Resource數(shù)組
?????? Spring還支持注入Resource數(shù)組,直接看配置如下:
<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”就不用多介紹了,傳統(tǒng)實(shí)現(xiàn)方式;對于“resourceBean2”則使用前綴“classpath*”,看到這大家應(yīng)該懂的,加載匹配多個(gè)資源;“resourceBean3”是混合使用的;測試代碼在“cn.javass.spring.chapter4.ResourceInjectTest.testResourceArrayInject”。
?????? Spring通過ResourceArrayPropertyEditor來進(jìn)行類型轉(zhuǎn)換的,而它又默認(rèn)使用“PathMatchingResourcePatternResolver”來進(jìn)行把路徑解析為Resource對象。所有大家只要會(huì)使用“PathMatchingResourcePatternResolver”,其它一些實(shí)現(xiàn)都是委托給它的,比如AppliacationContext的“getResources”方法等。
?
4.4.3? AppliacationContext實(shí)現(xiàn)對各種Resource的支持
???????一、ClassPathXmlApplicationContext:默認(rèn)將通過classpath進(jìn)行加載返回ClassPathResource,提供兩類構(gòu)造器方法:
public class ClassPathXmlApplicationContext { //1)通過ResourcePatternResolver實(shí)現(xiàn)根據(jù)configLocation獲取資源 public ClassPathXmlApplicationContext(String configLocation); public ClassPathXmlApplicationContext(String... configLocations); public ClassPathXmlApplicationContext(String[] configLocations, ……); //2)通過直接根據(jù)path直接返回ClasspathResource public ClassPathXmlApplicationContext(String path, Class clazz); public ClassPathXmlApplicationContext(String[] paths, Class clazz); public ClassPathXmlApplicationContext(String[] paths, Class clazz, ……); }
第一類構(gòu)造器是根據(jù)提供的配置文件路徑使用“ResourcePatternResolver ”的“getResources()”接口通過匹配獲取資源;即如“classpath:config.xml”
?????? 第二類構(gòu)造器則是根據(jù)提供的路徑和clazz來構(gòu)造ClassResource資源。即采用“public ClassPathResource(String path, Class<?> clazz)”構(gòu)造器獲取資源。
?
?
???????二、FileSystemXmlApplicationContext:將加載相對于當(dāng)前工作目錄的“configLocation”位置的資源,注意在linux系統(tǒng)上不管“configLocation”是否帶“/”,都作為相對路徑;而在window系統(tǒng)上如“D:/resourceInject.xml”是絕對路徑。因此在除非很必要的情況下,不建議使用該ApplicationContext。
public class FileSystemXmlApplicationContext{ public FileSystemXmlApplicationContext(String configLocation); public FileSystemXmlApplicationContext(String... configLocations,……); }//linux系統(tǒng),以下全是相對于當(dāng)前vm路徑進(jìn)行加載 new FileSystemXmlApplicationContext("chapter4/config.xml"); new FileSystemXmlApplicationContext("/chapter4/confg.xml");
//windows系統(tǒng),第一個(gè)將相對于當(dāng)前vm路徑進(jìn)行加載; //第二個(gè)則是絕對路徑方式加載 new FileSystemXmlApplicationContext("chapter4/config.xml"); new FileSystemXmlApplicationContext("d:/chapter4/confg.xml");
?此處還需要注意:在linux系統(tǒng)上,構(gòu)造器使用的是相對路徑,而ctx.getResource()方法如果以“/”開頭則表示獲取絕對路徑資源,而不帶前導(dǎo)“/”將返回相對路徑資源。如下:
//linux系統(tǒng),第一個(gè)將相對于當(dāng)前vm路徑進(jìn)行加載; //第二個(gè)則是絕對路徑方式加載 ctx.getResource ("chapter4/config.xml"); ctx.getResource ("/root/confg.xml"); //windows系統(tǒng),第一個(gè)將相對于當(dāng)前vm路徑進(jìn)行加載; //第二個(gè)則是絕對路徑方式加載 ctx.getResource ("chapter4/config.xml"); ctx.getResource ("d:/chapter4/confg.xml");?因此如果需要加載絕對路徑資源最好選擇前綴“file”方式,將全部根據(jù)絕對路徑加載。如在linux系統(tǒng)“ctx.getResource ("file:/root/confg.xml");”? ??
?
轉(zhuǎn)載于:https://www.cnblogs.com/achengmu/p/8440780.html
總結(jié)
以上是生活随笔為你收集整理的spring3: 4.4 使用路径通配符加载Resource的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: elasticsearch索引和映射
- 下一篇: 2-10日偶听某箴言