ResourceLoader接口
ResourceLoader接口用于返回Resource对象;其实现可以看作是一个生产Resource的工厂类。
public interface ResourceLoader {
Resource getResource(String location);
ClassLoader getClassLoader();
}
getResource接口用于根据提供的location参数返回相应的Resource对象;而getClassLoader则返回加载这些Resource的ClassLoader。
Spring提供了一个适用于所有环境的DefaultResourceLoader实现,可以返回ClassPathResource、UrlResource;还提供一个用于web环境的ServletContextResourceLoader,它继承了DefaultResourceLoader的所有功能,又额外提供了获取ServletContextResource的支持。
ResourceLoader在进行加载资源时需要使用前缀来指定需要加载:“classpath:path”表示返回ClasspathResource,“http://path”和“file:path”表示返回UrlResource资源,如果不加前缀则需要根据当前上下文来决定,DefaultResourceLoader默认实现可以加载classpath资源,看个例子:
@Test
public void testResourceLoad() {
ResourceLoader loader = new DefaultResourceLoader();
Resource resource = loader.getResource("classpath:com/spring/res/test1.txt");
//验证返回的是ClassPathResource
assertEquals(ClassPathResource.class, resource.getClass());
Resource resource2 = loader.getResource("file:com/spring/res/test1.txt");
//验证返回的是ClassPathResource
assertEquals(FileUrlResource.class, resource2.getClass());
Resource resource3 = loader.getResource("com/spring/res/test1.txt");
//验证返默认可以加载ClasspathResource
assertTrue(resource3 instanceof ClassPathResource);
}
对于目前所有ApplicationContext都实现了ResourceLoader,因此可以使用其来加载资源。
ClassPathXmlApplicationContext:不指定前缀将返回默认的ClassPathResource资源,否则将根据前缀来加载资源;
FileSystemXmlApplicationContext:不指定前缀将返回FileSystemResource,否则将根据前缀来加载资源;
WebApplicationContext:不指定前缀将返回ServletContextResource,否则将根据前缀来加载资源;
其他:不指定前缀根据当前上下文返回Resource实现,否则将根据前缀来加载资源。
ResourceLoaderAware接口
ResourceLoaderAware是一个标记接口,用于通过ApplicationContext上下文注入ResourceLoader。
public interface ResourceLoaderAware {
void setResourceLoader(ResourceLoader resourceLoader);
}
让我们看下测试代码吧:
1) 首先准备测试Bean,我们的测试Bean还简单只需实现ResourceLoaderAware接口,然后通过回调将ResourceLoader保存下来就可以了:
public class ResourceBean implements ResourceLoaderAware {
private ResourceLoader resourceLoader;
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public ResourceLoader getResourceLoader() {
return resourceLoader;
}
}
2) 配置Bean定义:
<bean class="com.spring.res.ResourceBean"/>
3)测试
@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("res/resourceLoaderAware.xml");
ResourceBean resourceBean = ctx.getBean(ResourceBean.class);
ResourceLoader loader = resourceBean.getResourceLoader();
Assert.assertTrue(loader instanceof ApplicationContext);
ResourceBean2 resourceBean2 = ctx.getBean(ResourceBean2.class);
Assert.assertTrue(resourceBean2.getResourceLoader() instanceof ApplicationContext);
}
注意此处“loader instanceof ApplicationContext”,说明了ApplicationContext就是个ResoureLoader。
由于上述实现回调接口注入ResourceLoader的方式属于侵入式,所以不推荐上述方法,可以采用更好的自动注入方式,如“byType”和“constructor”
注入Resource
通过回调或注入方式注入“ResourceLoader”,然后再通过“ResourceLoader”再来加载需要的资源对于只需要加载某个固定的资源是不是很麻烦,有没有更好的方法类似于前边实例中注入“java.io.File”类似方式呢?
Spring提供了一个PropertyEditor “ResourceEditor”用于在注入的字符串和Resource之间进行转换。因此可以使用注入方式注入Resource。
ResourceEditor完全使用ApplicationContext根据注入的路径字符串获取相应的Resource,说白了还是自己做还是容器帮你做的问题。
接下让我们看下示例:
1)准备Bean:
public class ResourceBean3 {
private Resource resource;
public Resource getResource() {
return resource;
}
public void setResource(Resource resource) {
this.resource = resource;
}
}
2)准备配置文件
<bean id="resourceBean1" class="com.spring.res.ResourceBean3">
<property name="resource" value="test1.properties"/>
</bean>
<bean id="resourceBean2" class="com.spring.res.ResourceBean3">
<property name="resource" value="classpath:test1.properties"/>
</bean>
注意此处“resourceBean1”注入的路径没有前缀表示根据使用的ApplicationContext实现进行选择Resource实现。
注入Resource数组
Spring还支持注入Resource数组,直接看配置如下:
<bean id="resourceBean1" class="com.spring.res.ResourceBean4">
<property name="resources">
<array>
<value>test1.properties</value>
<value>log4j.xml</value>
</array>
</property>
</bean>
<bean id="resourceBean2" class="com.spring.res.ResourceBean4">
<property name="resources" value="classpath*:META-INF/INDEX.LIST"/>
</bean>
<bean id="resourceBean3" class="com.spring.res.ResourceBean4">
<property name="resources">
<array>
<value>test1.properties</value>
<value>classpath*:META-INF/INDEX.LIST</value>
</array>
</property>
</bean>
使用路径通配符加载Resource
前面介绍的资源路径都是非常简单的一个路径匹配一个资源,Spring还提供了一种更强大的Ant模式通配符匹配,从能一个路径匹配一批资源。Spring提供AntPathMatcher来进行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在加载类路径资源时除了提供前缀“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 var1) throws IOException;
}
Spring提供了一个ResourcePatternResolver实现类——PathMatchingResourcePatternResolver,它是基于模式匹配的,默认使用AntPathMatcher进行路径匹配,它除了支持ResourceLoader支持的前缀外,还额外支持“classpath*:”用于加载所有匹配的类路径Resource。
首先做下准备工作,在项目的“resources”创建“META-INF”目录,然后在其下创建一个“INDEX.LIST”文件。
一、“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");
assertEquals(1, resources.length);
//只加载一个匹配的Resource,且通过ResourceLoader.getResource进行加载
resources = resolver.getResources("classpath:META-INF/*.LIST");
System.out.println(resources.length);
//只加载一个绝对匹配Resource,且通过ResourceLoader.getResource进行加载
resources = resolver.getResources("classpath:META-INF/MANIFEST.MF");
assertEquals(1, resources.length);
}
二、“classpath*”: 用于加载类路径(包括jar包)中的所有匹配的资源。带通配符的classpath使用“ClassLoader”的“<> ( 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");
assertTrue(resources.length > 1);
//将加载多个模式匹配的Resource
resources = resolver.getResources("classpath*:META-INF/*.LIST");
assertTrue(resources.length > 1);
}
三、“file”:加载一个或多个文件系统中的Resource。如“file:D:/*.txt”将返回D盘下的所有txt文件;
四、无前缀:通过ResourceLoader实现加载一个资源。
AppliacationContext提供的getResources方法将获取资源委托给ResourcePatternResolver实现,默认使用PathMatchingResourcePatternResolver。
AppliacationContext实现对各种Resource的支持
一、ClassPathXmlApplicationContext:默认将通过classpath进行加载返回ClassPathResource,提供两类构造器方法:
//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("res/config.xml");
new FileSystemXmlApplicationContext("/res/confg.xml");
//windows系统,第一个将相对于当前vm路径进行加载;
//第二个则是绝对路径方式加载
new FileSystemXmlApplicationContext("res/config.xml");
new FileSystemXmlApplicationContext("d:/res/confg.xml");
此处还需要注意:在linux系统上,构造器使用的是相对路径,而ctx.getResource()方法如果以“/”开头则表示获取绝对路径资源,而不带前导“/”将返回相对路径资源。如下:
//linux系统,第一个将相对于当前vm路径进行加载;
//第二个则是绝对路径方式加载
ctx.getResource ("res/config.xml");
ctx.getResource ("/root/confg.xml");
//windows系统,第一个将相对于当前vm路径进行加载;
//第二个则是绝对路径方式加载
ctx.getResource ("re/config.xml");
ctx.getResource ("d:/res/confg.xml");
因此如果需要加载绝对路径资源最好选择前缀“file”方式,将全部根据绝对路径加载。如在linux系统“ctx.getResource ("file:/root/confg.xml");”
参考:
http://sishuok.com/forum/blogPost/list/0/2458.html