spring resource通配符

public class ResourceBean3Controller{
	
	private Resource resource;
    public Resource getResource() {
        return resource;
    }
    public void setResource(Resource resource) {
        this.resource = resource;
    }
    
    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/resourceInject.xml");
        ResourceBean3Controller resourceBean1 = ctx.getBean("resourceBean1", ResourceBean3Controller.class);
        ResourceBean3Controller resourceBean2 = ctx.getBean("resourceBean2", ResourceBean3Controller.class);
        Assert.assertTrue(resourceBean1.getResource() instanceof ClassPathResource);
        Assert.assertTrue(resourceBean2.getResource() instanceof ClassPathResource);
    }
    
    
	/*使用路径通配符加载Resource
	Spring提供了一种更强大的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”,即把“**”当做两个“*”处理。
	*/
    @Test
    public void testClasspathPrefix()  {
    	ApplicationContext ctx = new ClassPathXmlApplicationContext("/resource*.xml");
        ResourceBean3Controller resourceBean1 = ctx.getBean("resourceBean1", ResourceBean3Controller.class);
        ResourceBean3Controller resourceBean2 = ctx.getBean("resourceBean2", ResourceBean3Controller.class);
        Assert.assertTrue(resourceBean1.getResource() instanceof ClassPathResource);
        Assert.assertTrue(resourceBean2.getResource() instanceof ClassPathResource);          
    }
    
    @Test
    public void testAsteriskPrefix () throws IOException {
         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();      
         //将加载多个绝对匹配的所有Resource
        //然后进行遍历模式匹配
        Resource[] resources=resolver.getResources("/*.properties");
        if(resources.length>0){
        	System.out.println(123);
        	dumpStream(resources[0]);
        }
        
        Assert.assertTrue(resources.length > 0);
        //将加载多个模式匹配的Resource
        resources = resolver.getResources("/log4j.properties");
        Assert.assertTrue(resources.length > 0);  
    }
    
    private void dumpStream(Resource resource) {
        InputStream is = null;
        try {
            //1.获取文件资源
            is = resource.getInputStream();
            //2.读取资源
            byte[] descBytes = new byte[is.available()];
            is.read(descBytes);
            System.out.println(new String(descBytes));
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                //3.关闭资源
                is.close();
            } catch (IOException e) {
            }
        }
    }
    
    /**
     * 注入
     */
    @Test
    public void testClasspathPrefix2()  {
    	ApplicationContext ctx = new ClassPathXmlApplicationContext("/bean3Resource*.xml");
        ResourceBean3Controller resourceBean1 = ctx.getBean("resourceBean1", ResourceBean3Controller.class);
        ResourceBean3Controller resourceBean2 = ctx.getBean("resourceBean2", ResourceBean3Controller.class);
        Assert.assertTrue(resourceBean1.getResource() instanceof ClassPathResource);
        Assert.assertTrue(resourceBean2.getResource() instanceof ClassPathResource);          
    }
     
}

<!-- Spring提供了一个PropertyEditor “ResourceEditor”用于在注入的字符串和Resource之间进行转换。
	因此可以使用注入方式注入Resource。	 -->
	<bean id="resourceBean1" class="com.boventech.learning.controller.ResourceBean3Controller">
	   <property name="resource" value="log4j.properties"/>
	</bean>
	<bean id="resourceBean2" class="com.boventech.learning.controller.ResourceBean3Controller"> 
		<property name="resource"
		value="classpath:log4j.properties"/>
	</bean>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值