Spring--ClassPathResource
- /*
- * 用一个给定的类加载器或者给定的类来加载资源
- */
- public class ClassPathResource extends AbstractFileResolvingResource {
- private final String path;
- private ClassLoader classLoader;
- private Class<?> clazz;
- /**
- * Create a new {@code ClassPathResource} for {@code 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 {@code ClassPathResource} for {@code 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 {@code ClassPathResource} for {@code 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 {@code ClassPathResource} with optional {@code ClassLoader}
- * and {@code 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.clazz != null ? this.clazz.getClassLoader() : this.classLoader);
- }
- /**
- * 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() {
- return (resolveURL() != null);
- }
- /**
- * Resolves a URL for the underlying class path resource.
- * @return the resolved URL, or {@code null} if not resolvable
- */
- //返回潜在的class 路径资源URL
- protected URL resolveURL() {
- if (this.clazz != null) {
- return this.clazz.getResource(this.path);
- }
- else if (this.classLoader != null) {
- return this.classLoader.getResource(this.path);
- }
- else {
- return ClassLoader.getSystemResource(this.path);
- }
- }
- /**
- * This implementation opens an InputStream for the given class path resource.
- * @see java.lang.ClassLoader#getResourceAsStream(String)
- * @see java.lang.Class#getResourceAsStream(String)
- */
- //
- @Override
- public InputStream getInputStream() throws IOException {
- InputStream is;
- if (this.clazz != null) {
- is = this.clazz.getResourceAsStream(this.path);
- }
- else if (this.classLoader != null) {
- is = this.classLoader.getResourceAsStream(this.path);
- }
- else {
- is = ClassLoader.getSystemResourceAsStream(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,
- * if available.
- * @see java.lang.ClassLoader#getResource(String)
- * @see java.lang.Class#getResource(String)
- */
- @Override
- public URL getURL() throws IOException {
- URL url = resolveURL();
- 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.
- */
- @Override
- 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();
- }
- }
Spring--ClassPathResource的更多相关文章
- 深入了解 Java Resource && Spring Resource
在Java中,为了从相对路径读取文件,经常会使用的方法便是: xxx.class.getResource(); xxx.class.getClassLoader().getResource(); 在S ...
- ajax图片上传及FastDFS入门案例.
今天来开始写图片上传的功能, 现在的图片上传都讲求 上传完成后立刻回显且页面不刷新, 这里到底是怎么做的呢? 当然是借助于ajax了, 但是ajax又不能提交表单, 这里我们还要借助一个插件: jqu ...
- JdbcTemplate源码解析
先写一个测试代码 package jdbc; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arr ...
- Spring之ClassPathResource加载资源文件
先看Demo: 1 @Test 2 public void testClassPathResource() throws IOException { 3 Resource res = new Clas ...
- 8 -- 深入使用Spring -- 3...1 Resource实现类ClassPathResource
8.3.1 Resource实现类------ClassPathResource : 访问类加载路径下的资源的实现类 2.访问类加载路径下的资源 ClassPathResource 用来访问类加载路径 ...
- 学习AOP之透过Spring的Ioc理解Advisor
花了几天时间来学习Spring,突然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另一方面要阅读源代码,这种方式理解起来事半功倍.那看书有什么用呢?主要还是扩展视野,毕竟书是别 ...
- 【初探Spring】------Spring IOC(三):初始化过程---Resource定位
我们知道Spring的IoC起到了一个容器的作用,其中装得都是各种各样的Bean.同时在我们刚刚开始学习Spring的时候都是通过xml文件来定义Bean,Spring会某种方式加载这些xml文件,然 ...
- 【初探Spring】------Spring IOC(二):初始化过程---简介
首先我们先来看看如下一段代码 ClassPathResource resource = new ClassPathResource("bean.xml"); DefaultList ...
- 【初探Spring】------Spring IOC(一)
IOC:Inversion of Control(控制反转).IOC它所体现的并不是一种技术,而是一种思想,一种将设计好的对象交给容器来管理的思想.IOC的核心思想就体现在控制.反转这两个词上面,要理 ...
- spring笔记5 spring IOC的基础知识1
1,ioc的概念 Inverse of control ,控制反转,实际的意义是调用类对接口实现类的依赖,反转给第三方的容器管理,从而实现松散耦合: ioc的实现方式有三种,属性注入,构造函数注入,接 ...
随机推荐
- [django]urls.py 中重定向
Django 1.5 有时候需要对一个链接直接重定向,比如首页啥的重定向到一个内容页等等,在views.py 中可以设定,如果没有参数啥的在urls.py 中设定更加方面 from django.vi ...
- wget 常用参数释义
wget 大法好啊,废话不多说,下面开始wget之旅吧. 下载限速 wget命令有一个内建的选项可以先顶下载任务占有的最大的带宽,从而保证其他应用程序的流畅运行. 具体使用--limit-rate 数 ...
- Android简易实战教程--第二十六话《网络图片查看器在本地缓存》
本篇接第二十五话 点击打开链接 http://blog.csdn.net/qq_32059827/article/details/52389856 上一篇已经把王略中的图片获取到了.生活中有这么 ...
- Linux命令—压缩及其他
(1)为了更好的传送和保存文件,需要对某些文件和目录进行压缩和解压缩操作,Linux 提供了强大的压缩.解压缩命令,常用的tar命令. (2)在Linux中,如果要使用储存设备(硬盘.光驱.移动 ...
- 一套强大的vim配置文件+详细注释
phpchina折腾王独家配置,灰常牛叉的一套vim配置,另附有详细注释,自己折腾vim的时候可以参照其中的大部分设置进行一些个性化定制."是否兼容VI,compatible为兼容,noco ...
- iOS开发之使用block块进行数据遍历的方法
看了一篇文章,发现遍历数组.字典中的数据时,除了使用for循环外,还可以使用block块进行操作,瞬间感觉iOS的语言代码确实有点高大上的感觉,下面就简单的介绍一下这个方法. 首先是最基本的运用形式, ...
- 6.0、Android Studio性能优化工具
显示图像包含四个步骤.简单来说,CPU对比显示列表,GPU渲染图片显示,内存存储图片和数据,电池提供点力能源.每个部分的硬件都有限制,超过这个限制会导致应用运行较慢,显示性能差,或者耗电. 为了查找造 ...
- 后端分布式系列:分布式存储-HDFS Client 设计实现解析
前面对 HDFS NameNode 和 DataNode 的架构设计实现要点做了介绍,本文对 HDFS 最后一个主要构成组件 Client 做进一步解析. 流式读取 HDFS Client 为客户端应 ...
- Android Service详解
service作为四大组件值得我们的更多的关注 在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务.例如,一个从service播放音乐的音乐播放器,应 ...
- 【UNIX网络编程第三版】阅读笔记(一):代码环境搭建
粗略的阅读过<TCP/IP详解>和<计算机网络(第五版)>后,开始啃这本<UNIX网络编程卷一:套接字联网API>,目前linux下的编程不算太了解,在阅读的过程中 ...