Resource(4)
来源:互联网 发布:linux vim 删除多行 编辑:程序博客网 时间:2024/06/15 23:05
使用通配符加载Resource
Spring提供了Ant模式通配符匹配,由一个路径匹配一个资源变为一个路径匹配一批资源。
Ant路径通配符支持“?”、“*”、“**”,注意通配符不包括目录分隔符“/”。
-“?”:匹配一个字符,如“config?.xml”匹配“config1.xml”、“config2.xml”、“config3.xml”等
-“*”:匹配零个或者多个字符串,如“com/*/config.xml”匹配“com/fsl/config.xml”
-“**”:匹配零个或者多个目录,如“com/**/config.xml”匹配“com/fsl/fsl1/config.xml”、“com/fsl/config.xml”
Spring在加载类路径资源时除了提供前缀“classpath:”来支持加载一个Resource,还添加了一个”classpath*:”来支持加载所有匹配的类路径Resource。
Spring提供ResourcePattrernResolver接口来加载多个Resource,该接口继承了ResourceLoader并添加了“ public abstract Resource[] getResources(String paramString) throws IOException;”来加载多个Resource;
Spring提供了一个ResourcePattrernResolver的实现PathMatchingResourcePattrernResolver;ResourcePattrernResolver除了支持ResourceLoader支持的前缀外还支持“classpath*:”,而Resource和ResourceLoader不支持“classpath*:”
“classpath:”
用于加载的类路径(包括jar包,找不到才去找jar包的)中的一个且仅一个资源;对于多个匹配也只返回一个“classpath*:”
用于加载的类路径(包括jar包)中的所有资源;使用“ClassLoader”的Enumeration<URL>getResource(String name)
方法来查找通配符之前的资源,然后才通过通配符来获取匹配的资源。如“classpath*:com/*/config.xml”会加载通配符之前的目录“com”,然后在遍历子路径进行匹配
ClassPathTest.java
package com.test;import java.io.IOException;import org.junit.Assert;import org.junit.Test;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver;public class ClassPathTest{@Testpublic void ClassPathTest()throws IOException{ ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver(); Resource[] resources; //单一匹配 resources=resolver.getResources("classpath:META-INF/license.txt"); Assert.assertTrue(1==resources.length); //多于一个也只返回一个 resources=resolver.getResources("classpath:META-INF/license*.txt"); Assert.assertTrue(resources.length==1); //多个Resource匹配 resources=resolver.getResources("classpath*:META-INF/license*.txt"); Assert.assertTrue(resources.length>1);}}
注意:这里匹配的是Spring jar包里的资源,Spring多个jar包下都有META-INF/license.txt
注意:如果jar包里的资源在jar包的根目录,“classptah*:”通配符加载不了,如”classpath*:config*.xml”通过通配符方式是加载不了config.xml。因为“ClassLoader”的“getResources(string name)”方法限制name为“”的情况只返回文件系统的类路径并不会返回jar包的跟路径。
ApplicationContext提供的getResources方法将获取资源委托给ResourcePatternResolver实现。默认使用PathMatchingResourcePattrernResolver
注入Resource数组
Bean
package com.resource;import org.springframework.core.io.Resource;public class ResourceBean { private Resource resource; private Resource[] resources; public Resource getResource() { return resource; } public void setResource(Resource resource) { this.resource = resource; } public Resource[] getResources() { return resources; } public void setResources(Resource[] resources) { this.resources = resources; }}
配置文件
<bean id="resourceBean3" class="com.resource.ResourceBean"> <property name="resources"> <array> <value>classpath*:META-INF/license*.txt</value> <!--导入了比较多的spring jar包 注释下面语句前数组长度是20 注释后是19 --> <!-- <value>applicationContextResource.xml</value> --> </array> </property> </bean>
测试类
package com.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.resource.ResourceBean;public class ResourceArrayTest { @Testpublic void ResourceArrayTest(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextResource.xml"); ResourceBean bean = context.getBean("resourceBean3", ResourceBean.class); System.out.println(bean.getResources().length);}}
- Resource(4)
- Spring入门4--Resource
- resource
- resource
- Resource
- Resource
- resource
- @Resource
- Resource
- $resource
- $resource
- Resource
- @Resource
- Resource
- @Resource
- resource
- resource
- @Resource
- Cannot reload AVD list: cvc-enumeration-valid: Value '280dpi' is ....问题解决
- Linux操作系统启动过程详解
- NYOJ 115城市平乱 【dijkstra】
- mysql 存储过程
- 所谓未来,就是现在
- Resource(4)
- wifi知识介绍
- 用户进程内存空间
- Andriod之EditText监听,实时的判断输入多少字符
- JDK 下载和安装
- App Store审核指南中文版
- Fragment笔记1
- AD09之器件封装编辑模式使用dimension
- transform 用法