您的位置:首页 > 编程语言 > Java开发

spring框架学习笔记(二)——spring的ioc之Resource

2013-11-25 19:48 671 查看
Spring的Resource接口位于包org.springframework.core.io中;

Spring定义Resource接口是为了提供更强的访问底层资源能力的抽象;

对spring来说Resource接口代表着物理存在的任何资源。

先看一下resource接口及其实现的类层次关系图:



对应的UML类图为:



其中,最常用的有四个:

ClassPathResource:通过 ClassPathResource 以类路径的方式进行访问;

FileSystemResource:通过 FileSystemResource 以文件系统绝对路径的方式进行访问;

ServletContextResource:通过 ServletContextResource 以相对于Web应用根目录的方式进行访问;

UrlResource :通过java.net.URL来访问资源,当然它也支持File格式,如“file:”。

1、先看一下Resource接口的定义:

Java代码



public interface Resource extends InputStreamSource { boolean exists(); boolean isReadable(); boolean isOpen(); URL getURL() throws IOException; URI getURI() throws IOException; File getFile() throws IOException; long contentLength() throws IOException; long lastModified() throws IOException; Resource createRelative(String relativePath) throws IOException; String getFilename(); String getDescription(); }

public interface Resource extends InputStreamSource {
boolean exists();
boolean isReadable();
boolean isOpen();
URL getURL() throws IOException;
URI getURI() throws IOException;
File getFile() throws IOException;
long contentLength() throws IOException;
long lastModified() throws IOException;
Resource createRelative(String relativePath) throws IOException;
String getFilename();
String getDescription();
}


它是spring访问资源最基本的接口。实际访问的时候直接用Resource接口就可以,不必使用其子类。其实经常用到的(resource的真正目的)方法是public InputStream getInputStream()

2、FileSystemResource

Java代码



public InputStream getInputStream() throws IOException { return new FileInputStream( this. file); }
public InputStream getInputStream() throws IOException {
return new FileInputStream( this. file);
}


这里的file是使用传入(构造函数中)的path生成的File,然后使用该File构造FileInputStream作为方法的输出。

这里的path一般要给出绝对路径,当然也可以是相对路径,如果是相对路径要注意其根目录。例如在eclipse中,它的根目录就是你工程目录作为你的根目录。

3、ClassPathResource

Java代码



public InputStream getInputStream() throws IOException {
InputStream is;
if ( this. clazz != null) {
is = this. clazz.getResourceAsStream( this. path);
}
else {
is = this. classLoader.getResourceAsStream( this. path);
}
if (is == null) {
throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");
}
return is;
}

public InputStream getInputStream() throws IOException {
InputStream is;
if ( this. clazz != null) {
is = this. clazz.getResourceAsStream( this. path);
}
else {
is = this. classLoader.getResourceAsStream( this. path);
}
if (is == null) {
throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");
}
return is;
}


这里是通过Class或者ClassLoader的getResourceAsStream()方法来获得InputStream的。其path一般都是以“classpath:”开头,如果以“classpath*:”开头表示所有与给定名称匹配的classpath资源都应该被获取。

4、ServletContextResource

Java代码



public InputStream getInputStream() throws IOException {
InputStream is = this.servletContext.getResourceAsStream(this.path);
if (is == null) {
throw new FileNotFoundException("Could not open " + getDescription());
}
return is;
}

public InputStream getInputStream() throws IOException {
InputStream is = this.servletContext.getResourceAsStream(this.path);
if (is == null) {
throw new FileNotFoundException("Could not open " + getDescription());
}
return is;
}


ServletContextResource通过ServletContext的getResourceAsStream()来取得InputStream,这里path必须以“/”开头,并且相对于当前上下文的根目录。如常用的path="/WEB-INF/web.xml"。

5、UrlResource

Java代码



public InputStream getInputStream() throws IOException { URLConnection con = this. url.openConnection(); ResourceUtils. useCachesIfNecessary(con); try { return con.getInputStream(); } catch (IOException ex) { // Close the HTTP connection (if applicable). if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).disconnect(); } throw ex; } }
public InputStream getInputStream() throws IOException {
URLConnection con = this. url.openConnection();
ResourceUtils. useCachesIfNecessary(con);
try {
return con.getInputStream();
}
catch (IOException ex) {
// Close the HTTP connection (if applicable).
if (con instanceof HttpURLConnection) {
((HttpURLConnection) con).disconnect();
}
throw ex;
}
}


UrlResource 封装了java.net.URL,它能够被用来访问任何通过URL可以获得的对象,例如:文件、HTTP对象、FTP对象等。

所有的URL都有个标准的 String表示,这些标准前缀可以标识不同的URL类型,包括file:访问文件系统路径,http: 通过HTTP协议访问的资源,ftp: 通过FTP访问的资源等等。

在Spring配置文件中,我们只需要给出字符串类型的path即可,Spring会通过ResourceEditor(java.beans.PropertyEditor的子类)和ResourceLoader把给定的path转换为相应的Resource。

转换规则如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: