Spring源码分析——资源访问利器Resource之实现类分析
今天来分析Spring的资源接口Resource的各个实现类。关于它的接口和抽象类,参见上一篇博文——Spring源码分析——资源访问利器Resource之接口和抽象类分析
一、文件系统资源 FileSystemResource
文件系统资源 FileSystemResource,资源以文件系统路径的方式表示。这个类继承自AbstractResource,并实现了写的接口WritableResource。类全称为public class FileSystemResource extends AbstractResource implements WritableResource 。这个资源类是所有Resource实现类中,唯一一个实现了WritableResource接口的类。就是说,其他的类都不可写入操作,都只能读取。部分翻译注释后,源码如下:(以后不重要的源码我就折叠起来)
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.core.io; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; /**
* @author Juergen Hoeller
* @since 28.12.2003
* @see java.io.File
*/
public class FileSystemResource extends AbstractResource implements WritableResource { private final File file; // 不可变属性 private final String path; // 不可变属性 public FileSystemResource(File file) { // 简单的构造方法,path为file路径格式化后的样子
Assert.notNull(file, "File must not be null");
this.file = file;
this.path = StringUtils.cleanPath(file.getPath());
} public FileSystemResource(String path) { //简单的构造方法
Assert.notNull(path, "Path must not be null");
this.file = new File(path);
this.path = StringUtils.cleanPath(path);
} public final String getPath() { //新增的方法,返回资源路径,方法不可重写
return this.path;
} @Override
public boolean exists() {
return this.file.exists();
} @Override
public boolean isReadable() {
return (this.file.canRead() && !this.file.isDirectory());
} public InputStream getInputStream() throws IOException { //InputStreamSource接口的实现方法
return new FileInputStream(this.file);
} @Override
public URL getURL() throws IOException { //可见这个url是通过uri得到的
return this.file.toURI().toURL();
} @Override
public URI getURI() throws IOException {
return this.file.toURI();
} @Override
public File getFile() {
return this.file;
} @Override
public long contentLength() throws IOException {
return this.file.length();
} @Override
public Resource createRelative(String relativePath) {
String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
return new FileSystemResource(pathToUse);
} @Override
public String getFilename() {
return this.file.getName();
} public String getDescription() { // 资源描述,直接用绝对路径来构造
return "file [" + this.file.getAbsolutePath() + "]";
} public boolean isWritable() { // WritableResource接口的实现方法
return (this.file.canWrite() && !this.file.isDirectory());
} public OutputStream getOutputStream() throws IOException {
return new FileOutputStream(this.file);
} @Override
public boolean equals(Object obj) { //通过path来比较
return (obj == this ||
(obj instanceof FileSystemResource && this.path.equals(((FileSystemResource) obj).path)));
} @Override
public int hashCode() { // 文件资源的HashCode就是path的hashCode
return this.path.hashCode();
} }
结论:
1、这个类由2个不可变的属性 file 和 path ,本质上就是一个java.io.File 的包装。
2、值得一提的是,与父类AbstractResource不同的是,这个类的 equals() 和 hashcode() 都通过属性 path 来操作。
测试:
public class FileSytemResourceTest {
public static void main(String[] args) {
String path = "E:/java/abc.txt";
Resource resource = new FileSystemResource(path);
System.out.println("resource1 : "+resource.getFilename()); File f = new File("text.txt");
Resource resource2 = new FileSystemResource(f);
System.out.println("resource2 : "+resource2.getFilename());
}
}
结果:
resource1 : abc.txt
resource2 : text.txt
二、字节数组资源——ByteArrayResource
字节数组资源ByteArrayResource,资源即,字节数组。
这个类很简单,也没必要翻译,仅仅是一个不可变的字节数组加一个不可变的描述字符串的包装,源码如下:
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.core.io; import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays; /**
* {@link Resource} implementation for a given byte array.
* Creates a ByteArrayInputStreams for the given byte array.
*
* <p>Useful for loading content from any given byte array,
* without having to resort to a single-use {@link InputStreamResource}.
* Particularly useful for creating mail attachments from local content,
* where JavaMail needs to be able to read the stream multiple times.
*
* @author Juergen Hoeller
* @since 1.2.3
* @see java.io.ByteArrayInputStream
* @see InputStreamResource
* @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource)
*/
public class ByteArrayResource extends AbstractResource { private final byte[] byteArray; private final String description; /**
* Create a new ByteArrayResource.
* @param byteArray the byte array to wrap
*/
public ByteArrayResource(byte[] byteArray) {
this(byteArray, "resource loaded from byte array");
} /**
* Create a new ByteArrayResource.
* @param byteArray the byte array to wrap
* @param description where the byte array comes from
*/
public ByteArrayResource(byte[] byteArray, String description) {
if (byteArray == null) {
throw new IllegalArgumentException("Byte array must not be null");
}
this.byteArray = byteArray;
this.description = (description != null ? description : "");
} /**
* Return the underlying byte array.
*/
public final byte[] getByteArray() {
return this.byteArray;
} /**
* This implementation always returns {@code true}.
*/
@Override
public boolean exists() {
return true;
} /**
* This implementation returns the length of the underlying byte array.
*/
@Override
public long contentLength() {
return this.byteArray.length;
} /**
* This implementation returns a ByteArrayInputStream for the
* underlying byte array.
* @see java.io.ByteArrayInputStream
*/
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(this.byteArray);
} /**
* This implementation returns the passed-in description, if any.
*/
public String getDescription() {
return this.description;
} /**
* This implementation compares the underlying byte array.
* @see java.util.Arrays#equals(byte[], byte[])
*/
@Override
public boolean equals(Object obj) {
return (obj == this ||
(obj instanceof ByteArrayResource && Arrays.equals(((ByteArrayResource) obj).byteArray, this.byteArray)));
} /**
* This implementation returns the hash code based on the
* underlying byte array.
*/
@Override
public int hashCode() {
return (byte[].class.hashCode() * 29 * this.byteArray.length);
} }
若需要操作描述一个字节数组,可以用这个资源类。ByteArrayResource可多次读取数组资源。
三、描述性资源——DescriptiveResource
描述性资源DescriptiveResource,这个类更简单,仅仅一个不可变的描述字符串的包装,源码如下:
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.core.io; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; /**
* Simple {@link Resource} implementation that holds a resource description
* but does not point to an actually readable resource.
*
* <p>To be used as placeholder if a {@code Resource} argument is
* expected by an API but not necessarily used for actual reading.
*
* @author Juergen Hoeller
* @since 1.2.6
*/
public class DescriptiveResource extends AbstractResource { private final String description; /**
* Create a new DescriptiveResource.
* @param description the resource description
*/
public DescriptiveResource(String description) {
this.description = (description != null ? description : "");
} @Override
public boolean exists() {
return false;
} @Override
public boolean isReadable() {
return false;
} public InputStream getInputStream() throws IOException {
throw new FileNotFoundException(
getDescription() + " cannot be opened because it does not point to a readable resource");
} public String getDescription() {
return this.description;
} /**
* This implementation compares the underlying description String.
*/
@Override
public boolean equals(Object obj) {
return (obj == this ||
(obj instanceof DescriptiveResource && ((DescriptiveResource) obj).description.equals(this.description)));
} /**
* This implementation returns the hash code of the underlying description String.
*/
@Override
public int hashCode() {
return this.description.hashCode();
} }
若一个资源,仅仅有一个描述,非常抽象的这种情况,可以用这个资源类,它并没有指向一个实际的可读的资源。一般用的非常稀少。个人觉得用处不大。
四、输入流资源——InputStreamResource
输入流资源InputStreamResource,是一个不可变InputStream的包装和一个不可变的描述字符串。此外还有一个私有成员变量Boolean read用于限制本资源的InputStream不可被重复获取。
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.core.io; import java.io.IOException;
import java.io.InputStream; /**
* {@link Resource} implementation for a given InputStream. Should only
* be used if no specific Resource implementation is applicable.
* In particular, prefer {@link ByteArrayResource} or any of the
* file-based Resource implementations where possible.
*
* <p>In contrast to other Resource implementations, this is a descriptor
* for an <i>already opened</i> resource - therefore returning "true" from
* {@code isOpen()}. Do not use it if you need to keep the resource
* descriptor somewhere, or if you need to read a stream multiple times.
*
* @author Juergen Hoeller
* @since 28.12.2003
* @see ByteArrayResource
* @see ClassPathResource
* @see FileSystemResource
* @see UrlResource
*/
public class InputStreamResource extends AbstractResource { private final InputStream inputStream; private final String description; private boolean read = false; /**
* Create a new InputStreamResource.
* @param inputStream the InputStream to use
*/
public InputStreamResource(InputStream inputStream) {
this(inputStream, "resource loaded through InputStream");
} /**
* Create a new InputStreamResource.
* @param inputStream the InputStream to use
* @param description where the InputStream comes from
*/
public InputStreamResource(InputStream inputStream, String description) {
if (inputStream == null) {
throw new IllegalArgumentException("InputStream must not be null");
}
this.inputStream = inputStream;
this.description = (description != null ? description : "");
} /**
* This implementation always returns {@code true}.
*/
@Override
public boolean exists() {
return true;
} /**
* This implementation always returns {@code true}.
*/
@Override
public boolean isOpen() {
return true;
} /**
* This implementation throws IllegalStateException if attempting to
* read the underlying stream multiple times.
*/
public InputStream getInputStream() throws IOException, IllegalStateException {
if (this.read) {
throw new IllegalStateException("InputStream has already been read - " +
"do not use InputStreamResource if a stream needs to be read multiple times");
}
this.read = true;
return this.inputStream;
} /**
* This implementation returns the passed-in description, if any.
*/
public String getDescription() {
return this.description;
} /**
* This implementation compares the underlying InputStream.
*/
@Override
public boolean equals(Object obj) {
return (obj == this ||
(obj instanceof InputStreamResource && ((InputStreamResource) obj).inputStream.equals(this.inputStream)));
} /**
* This implementation returns the hash code of the underlying InputStream.
*/
@Override
public int hashCode() {
return this.inputStream.hashCode();
} }
简单而言,这是一个InputStream的包装类,这个包装类指向的是一个已经打开的资源,所以它的 isOpen()总是返回true。而且它不能重复获取资源,只能读取一次。关闭资源也只能通过其中的InputStream来关闭。个人认为,用处有限。
五、VFS资源——VfsResource
vfs是Virtual File System虚拟文件系统,也称为虚拟文件系统开关(Virtual Filesystem Switch).是Linux档案系统对外的接口。任何要使用档案系统的程序都必须经由这层接口来使用它。(摘自百度百科...)它能一致的访问物理文件系统、jar资源、zip资源、war资源等,VFS能把这些资源一致的映射到一个目录上,访问它们就像访问物理文件资源一样,而其实这些资源不存在于物理文件系统。
这个资源类包装类一个Object对象,所有的操作都是通过这个包装的对象的反射来实现的。这里就没必要贴源码了。
可以参考下面的用法:
@Test
public void testVfsResourceForRealFileSystem() throws IOException {
//1.创建一个虚拟的文件目录
VirtualFile home = VFS.getChild("/home");
//2.将虚拟目录映射到物理的目录
VFS.mount(home, new RealFileSystem(new File("d:")));
//3.通过虚拟目录获取文件资源
VirtualFile testFile = home.getChild("test.txt");
//4.通过一致的接口访问
Resource resource = new VfsResource(testFile);
if(resource.exists()) {
dumpStream(resource);
}
System.out.println("path:" + resource.getFile().getAbsolutePath());
Assert.assertEquals(false, resource.isOpen());
}
@Test
public void testVfsResourceForJar() throws IOException {
//1.首先获取jar包路径
File realFile = new File("lib/org.springframework.beans-3.0.5.RELEASE.jar");
//2.创建一个虚拟的文件目录
VirtualFile home = VFS.getChild("/home2");
//3.将虚拟目录映射到物理的目录
VFS.mountZipExpanded(realFile, home,
TempFileProvider.create("tmp", Executors.newScheduledThreadPool(1)));
//4.通过虚拟目录获取文件资源
VirtualFile testFile = home.getChild("META-INF/spring.handlers");
Resource resource = new VfsResource(testFile);
if(resource.exists()) {
dumpStream(resource);
}
System.out.println("path:" + resource.getFile().getAbsolutePath());
Assert.assertEquals(false, resource.isOpen());
}
六、Portlet上下文资源——PortletContextResource
Portlet是基于java的web组件,由portlet容器管理,并由容器处理请求,生产动态内容。这个资源类封装了一个不可变的javax.portlet.PortletContext对象和一个不可变的String对象代表路径。类中所有操作都基于这两个属性。PortletContextResource对象实现了ContextResource接口,实现了方法String getPathWithinContext(),即返回自身的path属性。
源码如下:
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.web.portlet.context; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.portlet.PortletContext; import org.springframework.core.io.AbstractFileResolvingResource;
import org.springframework.core.io.ContextResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.portlet.util.PortletUtils; /**
* {@link org.springframework.core.io.Resource} implementation for
* {@link javax.portlet.PortletContext} resources, interpreting
* relative paths within the portlet application root directory.
*
* <p>Always supports stream access and URL access, but only allows
* {@code java.io.File} access when the portlet application archive
* is expanded.
*
* @author Juergen Hoeller
* @author John A. Lewis
* @since 2.0
* @see javax.portlet.PortletContext#getResourceAsStream
* @see javax.portlet.PortletContext#getRealPath
*/
public class PortletContextResource extends AbstractFileResolvingResource implements ContextResource { private final PortletContext portletContext; private final String path; /**
* Create a new PortletContextResource.
* <p>The Portlet spec requires that resource paths start with a slash,
* even if many containers accept paths without leading slash too.
* Consequently, the given path will be prepended with a slash if it
* doesn't already start with one.
* @param portletContext the PortletContext to load from
* @param path the path of the resource
*/
public PortletContextResource(PortletContext portletContext, String path) {
// check PortletContext
Assert.notNull(portletContext, "Cannot resolve PortletContextResource without PortletContext");
this.portletContext = portletContext; // check path
Assert.notNull(path, "Path is required");
String pathToUse = StringUtils.cleanPath(path);
if (!pathToUse.startsWith("/")) {
pathToUse = "/" + pathToUse;
}
this.path = pathToUse;
} /**
* Return the PortletContext for this resource.
*/
public final PortletContext getPortletContext() {
return this.portletContext;
} /**
* Return the path for this resource.
*/
public final String getPath() {
return this.path;
} /**
* This implementation checks {@code PortletContext.getResource}.
* @see javax.portlet.PortletContext#getResource(String)
*/
@Override
public boolean exists() {
try {
URL url = this.portletContext.getResource(this.path);
return (url != null);
}
catch (MalformedURLException ex) {
return false;
}
} /**
* This implementation delegates to {@code PortletContext.getResourceAsStream},
* which returns {@code null} in case of a non-readable resource (e.g. a directory).
* @see javax.portlet.PortletContext#getResourceAsStream(String)
*/
@Override
public boolean isReadable() {
InputStream is = this.portletContext.getResourceAsStream(this.path);
if (is != null) {
try {
is.close();
}
catch (IOException ex) {
// ignore
}
return true;
}
else {
return false;
}
} /**
* This implementation delegates to {@code PortletContext.getResourceAsStream},
* but throws a FileNotFoundException if not found.
* @see javax.portlet.PortletContext#getResourceAsStream(String)
*/
public InputStream getInputStream() throws IOException {
InputStream is = this.portletContext.getResourceAsStream(this.path);
if (is == null) {
throw new FileNotFoundException("Could not open " + getDescription());
}
return is;
} /**
* This implementation delegates to {@code PortletContext.getResource},
* but throws a FileNotFoundException if no resource found.
* @see javax.portlet.PortletContext#getResource(String)
*/
@Override
public URL getURL() throws IOException {
URL url = this.portletContext.getResource(this.path);
if (url == null) {
throw new FileNotFoundException(
getDescription() + " cannot be resolved to URL because it does not exist");
}
return url;
} /**
* This implementation resolves "file:" URLs or alternatively delegates to
* {@code PortletContext.getRealPath}, throwing a FileNotFoundException
* if not found or not resolvable.
* @see javax.portlet.PortletContext#getResource(String)
* @see javax.portlet.PortletContext#getRealPath(String)
*/
@Override
public File getFile() throws IOException {
URL url = getURL();
if (ResourceUtils.isFileURL(url)) {
// Proceed with file system resolution...
return super.getFile();
}
else {
String realPath = PortletUtils.getRealPath(this.portletContext, this.path);
return new File(realPath);
}
} @Override
public Resource createRelative(String relativePath) {
String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
return new PortletContextResource(this.portletContext, pathToUse);
} @Override
public String getFilename() {
return StringUtils.getFilename(this.path);
} public String getDescription() {
return "PortletContext resource [" + this.path + "]";
} public String getPathWithinContext() {
return this.path;
} @Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof PortletContextResource) {
PortletContextResource otherRes = (PortletContextResource) obj;
return (this.portletContext.equals(otherRes.portletContext) && this.path.equals(otherRes.path));
}
return false;
} @Override
public int hashCode() {
return this.path.hashCode();
} }
这个类非常简单,并没有什么需要注意的。
七、Servlet上下文资源——ServletContextResource
Servlet这个大家都知道。这个资源类是为了访问Web容器上下文的资源而封装的类,可以以相对于Web应用根目录的路径加载资源。这个资源类封装了一个不可变的javax.servlet.ServletContext对象和一个不可变的String对象代表路径。类中所有操作都基于这两个属性。PortletContextResource对象实现了ContextResource接口,实现了方法String getPathWithinContext(),即返回自身的path属性。
这个类的实现基本就是基于 this.servletContext.getResource(this.path) 或 this.servletContext.getResourceAsStream(this.path) 这两个方法。
典型的,例如这个方法:
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 URL getURL() throws IOException {
URL url = this.servletContext.getResource(this.path);
if (url == null) {
throw new FileNotFoundException(
getDescription() + " cannot be resolved to URL because it does not exist");
}
return url;
}
贴一下源码:
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.web.context.support; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletContext; import org.springframework.core.io.AbstractFileResolvingResource;
import org.springframework.core.io.ContextResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.util.WebUtils; /**
* {@link org.springframework.core.io.Resource} implementation for
* {@link javax.servlet.ServletContext} resources, interpreting
* relative paths within the web application root directory.
*
* <p>Always supports stream access and URL access, but only allows
* {@code java.io.File} access when the web application archive
* is expanded.
*
* @author Juergen Hoeller
* @since 28.12.2003
* @see javax.servlet.ServletContext#getResourceAsStream
* @see javax.servlet.ServletContext#getResource
* @see javax.servlet.ServletContext#getRealPath
*/
public class ServletContextResource extends AbstractFileResolvingResource implements ContextResource { private final ServletContext servletContext; private final String path; /**
* Create a new ServletContextResource.
* <p>The Servlet spec requires that resource paths start with a slash,
* even if many containers accept paths without leading slash too.
* Consequently, the given path will be prepended with a slash if it
* doesn't already start with one.
* @param servletContext the ServletContext to load from
* @param path the path of the resource
*/
public ServletContextResource(ServletContext servletContext, String path) {
// check ServletContext
Assert.notNull(servletContext, "Cannot resolve ServletContextResource without ServletContext");
this.servletContext = servletContext; // check path
Assert.notNull(path, "Path is required");
String pathToUse = StringUtils.cleanPath(path);
if (!pathToUse.startsWith("/")) {
pathToUse = "/" + pathToUse;
}
this.path = pathToUse;
} /**
* Return the ServletContext for this resource.
*/
public final ServletContext getServletContext() {
return this.servletContext;
} /**
* Return the path for this resource.
*/
public final String getPath() {
return this.path;
} /**
* This implementation checks {@code ServletContext.getResource}.
* @see javax.servlet.ServletContext#getResource(String)
*/
@Override
public boolean exists() {
try {
URL url = this.servletContext.getResource(this.path);
return (url != null);
}
catch (MalformedURLException ex) {
return false;
}
} /**
* This implementation delegates to {@code ServletContext.getResourceAsStream},
* which returns {@code null} in case of a non-readable resource (e.g. a directory).
* @see javax.servlet.ServletContext#getResourceAsStream(String)
*/
@Override
public boolean isReadable() {
InputStream is = this.servletContext.getResourceAsStream(this.path);
if (is != null) {
try {
is.close();
}
catch (IOException ex) {
// ignore
}
return true;
}
else {
return false;
}
} /**
* This implementation delegates to {@code ServletContext.getResourceAsStream},
* but throws a FileNotFoundException if no resource found.
* @see javax.servlet.ServletContext#getResourceAsStream(String)
*/
public InputStream getInputStream() throws IOException {
InputStream is = this.servletContext.getResourceAsStream(this.path);
if (is == null) {
throw new FileNotFoundException("Could not open " + getDescription());
}
return is;
} /**
* This implementation delegates to {@code ServletContext.getResource},
* but throws a FileNotFoundException if no resource found.
* @see javax.servlet.ServletContext#getResource(String)
*/
@Override
public URL getURL() throws IOException {
URL url = this.servletContext.getResource(this.path);
if (url == null) {
throw new FileNotFoundException(
getDescription() + " cannot be resolved to URL because it does not exist");
}
return url;
} /**
* This implementation resolves "file:" URLs or alternatively delegates to
* {@code ServletContext.getRealPath}, throwing a FileNotFoundException
* if not found or not resolvable.
* @see javax.servlet.ServletContext#getResource(String)
* @see javax.servlet.ServletContext#getRealPath(String)
*/
@Override
public File getFile() throws IOException {
URL url = this.servletContext.getResource(this.path);
if (url != null && ResourceUtils.isFileURL(url)) {
// Proceed with file system resolution...
return super.getFile();
}
else {
String realPath = WebUtils.getRealPath(this.servletContext, this.path);
return new File(realPath);
}
} /**
* This implementation creates a ServletContextResource, applying the given path
* relative to the path of the underlying file of this resource descriptor.
* @see org.springframework.util.StringUtils#applyRelativePath(String, String)
*/
@Override
public Resource createRelative(String relativePath) {
String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
return new ServletContextResource(this.servletContext, pathToUse);
} /**
* This implementation returns the name of the file that this ServletContext
* resource refers to.
* @see org.springframework.util.StringUtils#getFilename(String)
*/
@Override
public String getFilename() {
return StringUtils.getFilename(this.path);
} /**
* This implementation returns a description that includes the ServletContext
* resource location.
*/
public String getDescription() {
return "ServletContext resource [" + this.path + "]";
} public String getPathWithinContext() {
return this.path;
} /**
* This implementation compares the underlying ServletContext resource locations.
*/
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof ServletContextResource) {
ServletContextResource otherRes = (ServletContextResource) obj;
return (this.servletContext.equals(otherRes.servletContext) && this.path.equals(otherRes.path));
}
return false;
} /**
* This implementation returns the hash code of the underlying
* ServletContext resource location.
*/
@Override
public int hashCode() {
return this.path.hashCode();
} }
八、类路径资源——ClassPathResource
ClassPathResource这个资源类表示的是类路径下的资源,资源以相对于类路径的方式表示。这个资源类有3个成员变量,分别是一个不可变的相对路径、一个类加载器、一个类对象。这个资源类可以相对于应用程序下的某个类或者相对于整个应用程序,但只能是其中之一,取决于构造方法有没有传入Class参数。
这个类的实现基本也都是基于class的 getResourceAsStream(this.path) 或者 this.classLoader.getResourceAsStream(this.path) ,
我们来看一下它典型的getInputStream()方法:
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;
}
可以看到,本资源类的clazz属性存在,那么资源相对于这个clazz类相对路径的。如果不存在,那么资源类就是相对于整个应用程序的。
这里贴一下源码:
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.core.io; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; /**
* {@link Resource} implementation for class path resources.
* Uses either a given ClassLoader or a given Class for loading resources.
*
* <p>Supports resolution as {@code java.io.File} if the class path
* resource resides in the file system, but not for resources in a JAR.
* Always supports resolution as URL.
*
* @author Juergen Hoeller
* @author Sam Brannen
* @since 28.12.2003
* @see ClassLoader#getResourceAsStream(String)
* @see Class#getResourceAsStream(String)
*/
public class ClassPathResource extends AbstractFileResolvingResource { private final String path; private ClassLoader classLoader; private Class<?> clazz; /**
* Create a new ClassPathResource for ClassLoader usage.
* A leading slash will be removed, as the ClassLoader
* resource access methods will not accept it.
* <p>The thread context class loader will be used for
* loading the resource.
* @param path the absolute path within the class path
* @see java.lang.ClassLoader#getResourceAsStream(String)
* @see org.springframework.util.ClassUtils#getDefaultClassLoader()
*/
public ClassPathResource(String path) {
this(path, (ClassLoader) null);
} /**
* Create a new ClassPathResource for ClassLoader usage.
* A leading slash will be removed, as the ClassLoader
* resource access methods will not accept it.
* @param path the absolute path within the classpath
* @param classLoader the class loader to load the resource with,
* or {@code null} for the thread context class loader
* @see ClassLoader#getResourceAsStream(String)
*/
public ClassPathResource(String path, ClassLoader classLoader) {
Assert.notNull(path, "Path must not be null");
String pathToUse = StringUtils.cleanPath(path);
if (pathToUse.startsWith("/")) {
pathToUse = pathToUse.substring(1);
}
this.path = pathToUse;
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
} /**
* Create a new ClassPathResource for Class usage.
* The path can be relative to the given class,
* or absolute within the classpath via a leading slash.
* @param path relative or absolute path within the class path
* @param clazz the class to load resources with
* @see java.lang.Class#getResourceAsStream
*/
public ClassPathResource(String path, Class<?> clazz) {
Assert.notNull(path, "Path must not be null");
this.path = StringUtils.cleanPath(path);
this.clazz = clazz;
} /**
* Create a new ClassPathResource with optional ClassLoader and Class.
* Only for internal usage.
* @param path relative or absolute path within the classpath
* @param classLoader the class loader to load the resource with, if any
* @param clazz the class to load resources with, if any
*/
protected ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz) {
this.path = StringUtils.cleanPath(path);
this.classLoader = classLoader;
this.clazz = clazz;
} /**
* Return the path for this resource (as resource path within the class path).
*/
public final String getPath() {
return this.path;
} /**
* Return the ClassLoader that this resource will be obtained from.
*/
public final ClassLoader getClassLoader() {
return (this.classLoader != null ? this.classLoader : this.clazz.getClassLoader());
} /**
* This implementation checks for the resolution of a resource URL.
* @see java.lang.ClassLoader#getResource(String)
* @see java.lang.Class#getResource(String)
*/
@Override
public boolean exists() {
URL url;
if (this.clazz != null) {
url = this.clazz.getResource(this.path);
}
else {
url = this.classLoader.getResource(this.path);
}
return (url != null);
} /**
* This implementation opens an InputStream for the given class path resource.
* @see java.lang.ClassLoader#getResourceAsStream(String)
* @see java.lang.Class#getResourceAsStream(String)
*/
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;
} /**
* This implementation returns a URL for the underlying class path resource.
* @see java.lang.ClassLoader#getResource(String)
* @see java.lang.Class#getResource(String)
*/
@Override
public URL getURL() throws IOException {
URL url;
if (this.clazz != null) {
url = this.clazz.getResource(this.path);
}
else {
url = this.classLoader.getResource(this.path);
}
if (url == null) {
throw new FileNotFoundException(getDescription() + " cannot be resolved to URL because it does not exist");
}
return url;
} /**
* This implementation creates a ClassPathResource, applying the given path
* relative to the path of the underlying resource of this descriptor.
* @see org.springframework.util.StringUtils#applyRelativePath(String, String)
*/
@Override
public Resource createRelative(String relativePath) {
String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
return new ClassPathResource(pathToUse, this.classLoader, this.clazz);
} /**
* This implementation returns the name of the file that this class path
* resource refers to.
* @see org.springframework.util.StringUtils#getFilename(String)
*/
@Override
public String getFilename() {
return StringUtils.getFilename(this.path);
} /**
* This implementation returns a description that includes the class path location.
*/
public String getDescription() {
StringBuilder builder = new StringBuilder("class path resource [");
String pathToUse = path;
if (this.clazz != null && !pathToUse.startsWith("/")) {
builder.append(ClassUtils.classPackageAsResourcePath(this.clazz));
builder.append('/');
}
if (pathToUse.startsWith("/")) {
pathToUse = pathToUse.substring(1);
}
builder.append(pathToUse);
builder.append(']');
return builder.toString();
} /**
* This implementation compares the underlying class path locations.
*/
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof ClassPathResource) {
ClassPathResource otherRes = (ClassPathResource) obj;
return (this.path.equals(otherRes.path) &&
ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) &&
ObjectUtils.nullSafeEquals(this.clazz, otherRes.clazz));
}
return false;
} /**
* This implementation returns the hash code of the underlying
* class path location.
*/
@Override
public int hashCode() {
return this.path.hashCode();
} }
可见,需要操作应用程序类路径下的资源时,用这个资源类会非常方便。
九、Url资源——UrlResource
UrlResource这个资源类封装了可以以URL表示的各种资源。这个资源类有3个属性,一个URI、一个URL,以及一个规范化后的URL,用于资源间的比较以及计算HashCode。
通过构造方法可以看到,这个资源类基本可以看作java.net.URL的封装。这个资源类的很多方法也都是通过URL或URI操作的。
若是操作URL资源,很明显,这个类比单纯的java.net.URL要好很多。这个类总体很简单,重写父类的方法不是很多,不必多说。
源码:
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.core.io; import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection; import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils; /**
* {@link Resource} implementation for {@code java.net.URL} locators.
* Obviously supports resolution as URL, and also as File in case of
* the "file:" protocol.
*
* @author Juergen Hoeller
* @since 28.12.2003
* @see java.net.URL
*/
public class UrlResource extends AbstractFileResolvingResource { /**
* Original URI, if available; used for URI and File access.
*/
private final URI uri; /**
* Original URL, used for actual access.
*/
private final URL url; /**
* Cleaned URL (with normalized path), used for comparisons.
*/
private final URL cleanedUrl; /**
* Create a new UrlResource based on the given URI object.
* @param uri a URI
* @throws MalformedURLException if the given URL path is not valid
*/
public UrlResource(URI uri) throws MalformedURLException {
Assert.notNull(uri, "URI must not be null");
this.uri = uri;
this.url = uri.toURL();
this.cleanedUrl = getCleanedUrl(this.url, uri.toString());
} /**
* Create a new UrlResource based on the given URL object.
* @param url a URL
*/
public UrlResource(URL url) {
Assert.notNull(url, "URL must not be null");
this.url = url;
this.cleanedUrl = getCleanedUrl(this.url, url.toString());
this.uri = null;
} /**
* Create a new UrlResource based on a URL path.
* <p>Note: The given path needs to be pre-encoded if necessary.
* @param path a URL path
* @throws MalformedURLException if the given URL path is not valid
* @see java.net.URL#URL(String)
*/
public UrlResource(String path) throws MalformedURLException {
Assert.notNull(path, "Path must not be null");
this.uri = null;
this.url = new URL(path);
this.cleanedUrl = getCleanedUrl(this.url, path);
} /**
* Create a new UrlResource based on a URI specification.
* <p>The given parts will automatically get encoded if necessary.
* @param protocol the URL protocol to use (e.g. "jar" or "file" - without colon);
* also known as "scheme"
* @param location the location (e.g. the file path within that protocol);
* also known as "scheme-specific part"
* @throws MalformedURLException if the given URL specification is not valid
* @see java.net.URI#URI(String, String, String)
*/
public UrlResource(String protocol, String location) throws MalformedURLException {
this(protocol, location, null);
} /**
* Create a new UrlResource based on a URI specification.
* <p>The given parts will automatically get encoded if necessary.
* @param protocol the URL protocol to use (e.g. "jar" or "file" - without colon);
* also known as "scheme"
* @param location the location (e.g. the file path within that protocol);
* also known as "scheme-specific part"
* @param fragment the fragment within that location (e.g. anchor on an HTML page,
* as following after a "#" separator)
* @throws MalformedURLException if the given URL specification is not valid
* @see java.net.URI#URI(String, String, String)
*/
public UrlResource(String protocol, String location, String fragment) throws MalformedURLException {
try {
this.uri = new URI(protocol, location, fragment);
this.url = this.uri.toURL();
this.cleanedUrl = getCleanedUrl(this.url, this.uri.toString());
}
catch (URISyntaxException ex) {
MalformedURLException exToThrow = new MalformedURLException(ex.getMessage());
exToThrow.initCause(ex);
throw exToThrow;
}
} /**
* Determine a cleaned URL for the given original URL.
* @param originalUrl the original URL
* @param originalPath the original URL path
* @return the cleaned URL
* @see org.springframework.util.StringUtils#cleanPath
*/
private URL getCleanedUrl(URL originalUrl, String originalPath) {
try {
return new URL(StringUtils.cleanPath(originalPath));
}
catch (MalformedURLException ex) {
// Cleaned URL path cannot be converted to URL
// -> take original URL.
return originalUrl;
}
} /**
* This implementation opens an InputStream for the given URL.
* It sets the "UseCaches" flag to {@code false},
* mainly to avoid jar file locking on Windows.
* @see java.net.URL#openConnection()
* @see java.net.URLConnection#setUseCaches(boolean)
* @see java.net.URLConnection#getInputStream()
*/
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;
}
} /**
* This implementation returns the underlying URL reference.
*/
@Override
public URL getURL() throws IOException {
return this.url;
} /**
* This implementation returns the underlying URI directly,
* if possible.
*/
@Override
public URI getURI() throws IOException {
if (this.uri != null) {
return this.uri;
}
else {
return super.getURI();
}
} /**
* This implementation returns a File reference for the underlying URL/URI,
* provided that it refers to a file in the file system.
* @see org.springframework.util.ResourceUtils#getFile(java.net.URL, String)
*/
@Override
public File getFile() throws IOException {
if (this.uri != null) {
return super.getFile(this.uri);
}
else {
return super.getFile();
}
} /**
* This implementation creates a UrlResource, applying the given path
* relative to the path of the underlying URL of this resource descriptor.
* @see java.net.URL#URL(java.net.URL, String)
*/
@Override
public Resource createRelative(String relativePath) throws MalformedURLException {
if (relativePath.startsWith("/")) {
relativePath = relativePath.substring(1);
}
return new UrlResource(new URL(this.url, relativePath));
} /**
* This implementation returns the name of the file that this URL refers to.
* @see java.net.URL#getFile()
* @see java.io.File#getName()
*/
@Override
public String getFilename() {
return new File(this.url.getFile()).getName();
} /**
* This implementation returns a description that includes the URL.
*/
public String getDescription() {
return "URL [" + this.url + "]";
} /**
* This implementation compares the underlying URL references.
*/
@Override
public boolean equals(Object obj) {
return (obj == this ||
(obj instanceof UrlResource && this.cleanedUrl.equals(((UrlResource) obj).cleanedUrl)));
} /**
* This implementation returns the hash code of the underlying URL reference.
*/
@Override
public int hashCode() {
return this.cleanedUrl.hashCode();
} }
十、对资源编码——EncodedResource
EncodedResource并不是Resource的实现类,实际上它相当于一个工具类,用于给资源进行编码。
由于资源加载时默认采用系统编码读取资源内容,需要给Resource编码时可以使用这个包装工具类。它的核心为一个 java.io.Reader getReader()方法。
这个类有一个不可变的Resource属性、一个Charset属性和一个表示字符的string属性。后两者存在一个即可。我们来看一下getReader()的实现:
public Reader getReader() throws IOException {
if (this.charset != null) {
return new InputStreamReader(this.resource.getInputStream(), this.charset);
}
else if (this.encoding != null) {
return new InputStreamReader(this.resource.getInputStream(), this.encoding);
}
else {
return new InputStreamReader(this.resource.getInputStream());
}
}
Resource这个资源接口大体就是这样了。总体上感觉并不复杂。一般讲Resource,基本都会讲一个跟Resouce紧密相关的接口ResourceLoader,用来方便的载入Resouce。而由于ApplicationContext这些接口都是ResouceLoader的子接口,也实现类资源加载的功能。所以我打算分析完Spring容器类之后再来讲这个。
阅读Resouce源码最大的感悟就是,Resouce的工具类实在好强大,而且,Spring的编写者对于JDK真的好熟悉,有机会必须分析分析JDK源码才行。
Spring源码分析——资源访问利器Resource之实现类分析的更多相关文章
- Spring源码分析——资源访问利器Resource之接口和抽象类分析
从今天开始,一步步走上源码分析的路.刚开始肯定要从简单着手.我们先从Java发展史上最强大的框架——Spring...旗下的资源抽象接口Resource开始吧. 我看了好多分析Spring源码的,每每 ...
- 攻城狮在路上(贰) Spring(三)--- Spring 资源访问利器Resource接口
Spring为了更好的满足各种底层资源的访问需求.设计了一个Resource接口,提供了更强的访问底层资源的能力.Spring框架使用Resource装载各种资源,包括配置文件资源.国际化属性文件资源 ...
- Spring源码分析-BeanFactoryPostProcessors 应用之 PropertyPlaceholderConfigurer
BeanFactoryPostProcessors 介绍 BeanFactoryPostProcessors完整定义: /** * Allows for custom modification of ...
- Spring源码情操陶冶-自定义节点的解析
本文承接前文Spring源码情操陶冶-DefaultBeanDefinitionDocumentReader#parseBeanDefinitions,特开辟出一块新地来啃啃这块有意思的骨头 自定义节 ...
- Spring源码阅读 ------------------- SpringFrameWork 5.2 +IDEA 部署
Spring作为JAVA学习者的必须学习和熟悉的框架,需要理解Spring的设计原理,核心设计模式,对于理解设计模式,面向对象的深入理解有着深远持久的影响,特此首先要做到本地部署源码开始,下面将介绍如 ...
- 【spring源码分析】IOC容器初始化(二)
前言:在[spring源码分析]IOC容器初始化(一)文末中已经提出loadBeanDefinitions(DefaultListableBeanFactory)的重要性,本文将以此为切入点继续分析. ...
- spring源码分析系列 (5) spring BeanFactoryPostProcessor拓展类PropertyPlaceholderConfigurer、PropertySourcesPlaceholderConfigurer解析
更多文章点击--spring源码分析系列 主要分析内容: 1.拓展类简述: 拓展类使用demo和自定义替换符号 2.继承图UML解析和源码分析 (源码基于spring 5.1.3.RELEASE分析) ...
- Spring源码分析(八)AbstractBeanDefinition属性
摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 在上一篇中已经完成了XML文档到GenericBeanDefiniton ...
- Spring源码解析02:Spring IOC容器之XmlBeanFactory启动流程分析和源码解析
一. 前言 Spring容器主要分为两类BeanFactory和ApplicationContext,后者是基于前者的功能扩展,也就是一个基础容器和一个高级容器的区别.本篇就以BeanFactory基 ...
随机推荐
- JMeter专题系列(六)集合点
JMeter也有像LR中的集合点: JMeter里面的集合点是通过添加定时器来完成. 注意:集合点的位置一定要在Sample之前. 集合点:虽然我们的“性能测试”理解为“多用户并发测试”,但客观上来说 ...
- codis集群安装
在网上找了很多codis的集群安装方法,看起来都是大同小异,本人结合了大多种方法完成了一套自己使用的codis的集群安装,可以供大家学习使用,如果有什么问题或者不懂的地方欢迎指正 1.集群规划: 三台 ...
- 开窗函数使用及sql自行构建枚举数据用于关联
1, SELECT * FROM ( SELECT ROW_NUMBER() OVER ( PARTITION BY process_instance_id (区分相似数据的字段,逗号分 ...
- 手机浏览器不支持 IDBObjectStore.getAll
最近在学习IndexDB,使用了IDBObjectStore.getAll,发现手机上不支持. 后面,查阅了mdn:  的确是不支持,且可以看到这个函数现在兼容性很差. 解决方法: 1.使用 IDB ...
- 奇妙的CSS之布局与定位
前言 关于布局与定位是Web前端开发里非常基础而又重要的部分.介绍相关知识的文章,很容易就可以找到.虽然,这方面的知识点不是很多,但我们如果不弄清楚,在运用时候往往会出现预料之外的布局,这些“意外”有 ...
- iOS项目groups和folder的区别(组和文件夹)
在引用一个第三方框架的时候,已经拖进去了,但是引用框架里面的文件时,竟然报错说找不到.......查了一下,原来在拖进去时没有注意group和folder的选择! 其实仔细观察一下,不难发现,以gro ...
- android 双缓存机制
废话不多说,直接贴代码! 所谓的双缓存,第一就是缓存在内存里面,第二就是缓存在SD卡里面,当你需要加载数据时,先去内存缓存中查找,如果没有再去SD卡中查找,并且用户可以自选使用哪种缓存! 缓存内存和缓 ...
- iOS之触摸及手势
触摸事件 iOS中的事件: 在用户使用app过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: view的触摸事件处理: 响应者对象: 在iOS中不是任何对象都能处理事件,只有继承了 ...
- svn的使用(转载)
这里只介绍使用CornerStone来使用SVN. CornerStone是Mac OS X系统下非常好用的一款svn工具,当然还有Versions也是可以用的,但是使用起来不如CornerStone ...
- ImageView的ScaleType属性
ImageView的ScaleType各种值代表的意义: CENTER:不进行任何缩放,将图片放在容器中间 CENTER_CROP:如果图片长宽都大于等于容器长宽,则图片不缩放,否则按固定长宽比缩放, ...