Spring之ClassPathResource加载资源文件
先看Demo:
1 @Test
2 public void testClassPathResource() throws IOException {
3 Resource res = new ClassPathResource("resource/ApplicationContext.xml");
4 InputStream input = res.getInputStream();
5 Assert.assertNotNull(input);
6 }
再看内部源码:
1 public ClassPathResource(String path) {
2 this(path, (ClassLoader) null);
3 }

1 public ClassPathResource(String path, ClassLoader classLoader) {
2 Assert.notNull(path, "Path must not be null");
3 String pathToUse = StringUtils.cleanPath(path);
4 if (pathToUse.startsWith("/")) {
5 pathToUse = pathToUse.substring(1);
6 }
7 this.path = pathToUse;
8 this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
9 }

1 public ClassPathResource(String path, Class<?> clazz) {
2 Assert.notNull(path, "Path must not be null");
3 this.path = StringUtils.cleanPath(path);
4 this.clazz = clazz;
5 }
获取资源内容:

1 /**
2 * This implementation opens an InputStream for the given class path resource.
3 * @see java.lang.ClassLoader#getResourceAsStream(String)
4 * @see java.lang.Class#getResourceAsStream(String)
5 */
6 @Override
7 public InputStream getInputStream() throws IOException {
8 InputStream is;
9 if (this.clazz != null) {
10 is = this.clazz.getResourceAsStream(this.path);
11 }
12 else if (this.classLoader != null) {
13 is = this.classLoader.getResourceAsStream(this.path);
14 }
15 else {
16 is = ClassLoader.getSystemResourceAsStream(this.path);
17 }
18 if (is == null) {
19 throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");
20 }
21 return is;
22 }

源码解读:
该类获取资源的方式有两种:Class获取和ClassLoader获取。
两种方法的区别:
再看Demo:

1 @Test
2 public void testResouce() {
3 ClassLoader loader = Thread.currentThread().getContextClassLoader();
4 System.out.println(loader.getResource("").getPath());
5
6 System.out.println(this.getClass().getResource("").getPath());
7 System.out.println(this.getClass().getResource("/").getPath());
8
9 System.out.println(System.getProperty("user.dir"));
10 }

运行结果:
|
1
2
3
4
|
/home/sunny/workspace/spring-01/target/test-classes//home/sunny/workspace/spring-01/target/test-classes/com/me/spring/spring_01//home/sunny/workspace/spring-01/target/test-classes//home/sunny/workspace/spring-01 |
Class.getResource("")获取的是相对于当前类的相对路径
Class.getResource("/")获取的是classpath的根路径
ClassLoader.getResource("")获取的是classpath的根路径
在创建ClassPathResource对象时,我们可以指定是按Class的相对路径获取文件还是按ClassLoader来获取。
Spring之ClassPathResource加载资源文件的更多相关文章
- Spring boot 国际化自动加载资源文件问题
Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...
- spring 如何动态加载properties文件的内容
1. 在xml中配置你的properties路径: <bean id="messageSource" class="org.springframework.cont ...
- Maven,预加载资源文件
预加载资源文件需要先启用功能: <build> <resources> <resource> <directory>src/main/resources ...
- Java加载资源文件的两种方法
处理配置文件对于Java程序员来说再常见不过了,不管是Servlet,Spring,抑或是Structs,都需要与配置文件打交道.Java将配置文件当作一种资源(resource)来处理,并且提供了两 ...
- Spring 启动时加载资源
Spring加载资源文件目前了解三种, @PostConstruct在Context加载完成之后加载.在创建各个Bean对象之前加载. 实现ApplicationRunner的run方法,Bean加载 ...
- 动态加载资源文件(ResourceDictionary)
原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...
- Style样式的四种使用(包括用C#代码动态加载资源文件并设置样式)
Posted on 2012-03-23 11:21 祥叔 阅读(2886) 评论(6) 编辑 收藏 在Web开发中,我们通过CSS来控制页面元素的样式,一般常用三种方式: 1. 内联样式 ...
- PyQt5(5)——加载资源文件
在实际中我们需要美化界面,就需要许多的自定义图片. 但是我们发现直接导入图像使用,等程序运行时会报错.???? 这就需要建立资源文件并且加载它们,程序就可以顺利运行了. 设计界面是如何加载资源文件呢? ...
- spring加载资源文件中classpath*与classpath的区别
在spring和MyBatis继承的时候,配置mapperLocations.一开始配置是这样的. 需要加载路径为com/thomas/base/mapper和com/thomas/bu/mapper ...
随机推荐
- 极光推送Jpush(v3)服务端PHP版本的api脚本类
原文地址:http://www.dodobook.net/php/780 关于极光推送的上一篇文章已经说明了,此处就不多说了.使用v3版本的原因是v2使用到2014年年底就停止了.点击查看上一篇的地址 ...
- 解决国内npm依赖包安装慢的问题
可以通过 npm config set registry http://registry.npm.taobao.org命令设置镜像服务器为阿里巴巴镜像服务器,速度会快很多,亲测有效.
- js检测手机摇一摇
1.检测设备是否支持重力感应事件deviceorientation deviceorientation 提供设备的物理方向信息,表示为一系列本地坐标系的旋角 function motionHandle ...
- openfire xmpp 登录参数解析
1.openfire xmpp登录 boolean result = false; ConnectionConfiguration config = new ConnectionConfigurati ...
- IIC总线
一. 概述 1. IIC总线是PHILIPS公司推出的一种串口总线,是具备多主机系统所需的包括总线裁决和高低速器件同步功能的高性能串口总线. 2. IIC总线只有两根双向信号线.一根是数据线SDA,一 ...
- TortoiseGit安装详解
一:写该文章目的 最近换了一份新工作,新公司的源码管理都是使用GIT,习惯了之前的TFS和SVN进行项目源码管理和团队开发,第一次使用GIT进行团队开发和源码管理,颇有一些不习惯,花了一天时间终于把G ...
- oracle查询某个时间点的数据
1. select * from emps as of timestamp to_Date('2015-12-11 14:00:00','yyyy-mm-dd hh24:mi:ss'),SQL语句是查 ...
- 在使用Intelligencia.UrlRewriter过程中 中文乱码问题
由于业务需求,最近将项目部分模块修改为伪静态,使用到了Intelligencia.UrlRewriter.dll组件. 网上对使用Intelligencia.UrlRewriter.dll的配置讲解很 ...
- Smokeping安装教程
Smokeping安装教程 #Smokeping2.6.8安装教程 #2016.3.6 改编v1.0 #Linux运维技术交流 347163978 环境 CentOS release 6.4 (F ...
- .NET编译项目时出现《此实现不是 Windows 平台 FIPS 验证的加密算法的一部分》处理方法
有用户提出在编译代码时出现源文件“D:\.......ervice.cs”未能打开(“此实现不是 Windows 平台 FIPS 验证的加密算法的一部分.”)的问题,如下图所示: 对于上面的问题,只需 ...