spring 学习笔记-----访问资源

[b]访问资源的核心:Resource[/b]
这是一个接口,这个接口定义了7种方法。
boolean exists();
boolean isOpen();
URL getURL();
File getFile();
String getFilename();
String getDescription();
Resource createRelative(String relativePath);

在spring内部,spring是使用了另外一个接口,ResourceLoader以及缺省实现DefaultResourceLoader来定位和创建Resource资源。一般是不会和DefaultResourceLoader打交道的,是用的是另一个实现------ApplicationContext。
下面是个访问三种资源的例子:
/*
* Created on 07-Aug-2004
*/
package com.apress.prospring.ch5.resource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.Resource;

/**
* @author robh
*/
public class ResourceDemo {

public static void main(String[] args) throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"./ch5/src/conf/events/events.xml");

Resource res1 = ctx.getResource("file:///D:\\学习笔记\\学习心得\\学习心得.doc");
displayInfo(res1);
//Resource res2 = ctx.getResource("classpath:lib/commons-logging.jar");
//displayInfo(res2);
Resource res3 = ctx.getResource("http://www.google.co.uk");
displayInfo(res3);
}

private static void displayInfo(Resource res) throws Exception {
System.out.println(res.getClass());
System.out.println(res.getURL().getContent());
System.out.println("");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值