使用路徑萬用字元載入Resource,
阿新 • • 發佈:2019-01-28
http://blog.csdn.net/liufengyinglxj/article/details/8646417(這個比較好)
下面又是一個:
spring提供了強大的Ant模式萬用字元匹配,從同一個路徑能匹配一批資源。
Ant路徑萬用字元支援"?"、"*"、"**",注意萬用字元匹配不包括目錄分隔符“/”。
“?”:匹配一個字元,如"config?.xml"可匹配"config1.xml".
“*”:匹配零個或多個字串,如“com/*/config.xml”將匹配“cn/feng/config.xml”,但不匹配匹配“com/config.xml”(因為這裡匹配的是字串,如果是目錄的話則可以);而“com/config-*.xml”將匹配“com/config-dao.xml”;
"**":匹配路徑中的零個或多個目錄。如“com/**/config.xml”將匹配“com/config.xml”,也匹配“com/feng/spring/config.xml”;而“com/feng/config-**.xml”將匹配“com/feng/config-dao.xml”,即把“**”當做兩個“*”處理。
Spring在載入類路徑資源時除了提供字首“classpath:”的來支援載入一個Resource,還提供一個字首“classpath*:”來支援載入所有匹配的類路徑Resource。
Spring提供了ResourcePatternResolver介面來載入多個Resource.
[java] view plain copy
package com.feng.spring.chapter2.helloworld;
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public interface ResourcePatternResolver extends ResourceLoader{
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
Resource[] getResources(String locationPattern) throws IOException;//添加了此方法用來接收多個Resource
}
一、"classpath":用於載入類路徑(包括jar包)中的一個且僅一個資源;對於多個匹配的也只返回一個。如果需要多個匹配的則考慮"classpath*."字首。
[java] view plain copy
package com.feng.spring.chapter2.helloworld;
import java.io.IOException;
import org.junit.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
public class HelloTest {
@Test
public void testClasspathPrefix()throws IOException{
ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
//只加載一個絕對匹配Resource,且通過ResourceLoader.getResource進行載入
Resource resources=resolver.getResource("classpath:META-INF/INDEX.LIST");
Assert.assertEquals(1, resources.length);
//只加載一個匹配的Resource,且通過ResourceLoader.getResource進行載入
resources = resolver.getResource("classpath:META-INF/*.LIST");
Assert.assertTrue(resources.length == 1);
}
}
二、"classpath*":
用於載入類路徑(包括jar包)中所有的匹配的資源。
[java] view plain copy
package com.feng.spring.chapter2.helloworld;
import java.io.IOException;
import javax.annotation.Resource;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
public class HelloTest {
@Test
public void testClasspathAsteriskPrefix()throws IOException{
ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
//將載入多個絕對匹配的所有Resource
//將首先通過ClassLoader.getResource("META-INF")載入非模式路徑部分
//然後進行遍歷模式匹配
Resource[] resources = (Resource[]) resolver.getResources("classpath*:META-INF/INDEX.LIST");
Assert.assertTrue(resources.length > 1);
//將載入多個模式匹配的Resource
resources = (Resource[]) resolver.getResources("classpath*:META-INF/*.LIST");
Assert.assertTrue(resources.length > 1);
}
}
帶萬用字元的classpath使用“ClassLoader”的“Enumeration<URL> getResources(String name)”方法來查詢萬用字元之前的資源,然後通過模式匹配來獲取匹配的資源。如“classpath:META-INF/*.LIST”將首先載入萬用字元之前的目錄“META-INF”,然後再遍歷路徑進行子路徑匹配從而獲取匹配的資源。
三、"file":載入一個或多個系統中的Resource。如:"file:D/*.txt"將返回D盤下的所有txt檔案。
四、無字首:通過ResourceLoader實現載入一個資源。
ApplicationContext提供的getResource方法將獲取資源委託給ResourcePatternResolver實現,預設使用PathMatingResourcePatternResolver.