Resource(4)

使用通配符加载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*:”

  1. “classpath:”
    用于加载的类路径(包括jar包,找不到才去找jar包的)中的一个且仅一个资源;对于多个匹配也只返回一个

  2. “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{
@Test
public 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 {
    @Test
public void ResourceArrayTest(){
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextResource.xml");
    ResourceBean bean = context.getBean("resourceBean3", ResourceBean.class);
    System.out.println(bean.getResources().length);

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值