先看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加载资源文件的更多相关文章

  1. Spring boot 国际化自动加载资源文件问题

    Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...

  2. spring 如何动态加载properties文件的内容

    1. 在xml中配置你的properties路径: <bean id="messageSource" class="org.springframework.cont ...

  3. Maven,预加载资源文件

    预加载资源文件需要先启用功能: <build> <resources> <resource> <directory>src/main/resources ...

  4. Java加载资源文件的两种方法

    处理配置文件对于Java程序员来说再常见不过了,不管是Servlet,Spring,抑或是Structs,都需要与配置文件打交道.Java将配置文件当作一种资源(resource)来处理,并且提供了两 ...

  5. Spring 启动时加载资源

    Spring加载资源文件目前了解三种, @PostConstruct在Context加载完成之后加载.在创建各个Bean对象之前加载. 实现ApplicationRunner的run方法,Bean加载 ...

  6. 动态加载资源文件(ResourceDictionary)

    原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...

  7. Style样式的四种使用(包括用C#代码动态加载资源文件并设置样式)

    Posted on 2012-03-23 11:21 祥叔 阅读(2886) 评论(6) 编辑 收藏 在Web开发中,我们通过CSS来控制页面元素的样式,一般常用三种方式: 1.       内联样式 ...

  8. PyQt5(5)——加载资源文件

    在实际中我们需要美化界面,就需要许多的自定义图片. 但是我们发现直接导入图像使用,等程序运行时会报错.???? 这就需要建立资源文件并且加载它们,程序就可以顺利运行了. 设计界面是如何加载资源文件呢? ...

  9. spring加载资源文件中classpath*与classpath的区别

    在spring和MyBatis继承的时候,配置mapperLocations.一开始配置是这样的. 需要加载路径为com/thomas/base/mapper和com/thomas/bu/mapper ...

随机推荐

  1. php如何查看变量是真实被引用

    $var1 = 'Hello World'; $var2 = ''; $var2 =&$var1; debug_zval_dump(&$var1); $a = "aaa&qu ...

  2. PHP 模拟 HTTP 基本认证(Basic Authentication)

    当某个页面需要认证才能进行访问时,接到请求后服务器端会在响应头中发送一个 WWW-Authenticate 首部(用来标识认证安全域),语法为 WWW-Authenticate:Basic relam ...

  3. Java工具Eclipse

    一.下载Eclipse 从官网渠道下载或从公司共享软件目录下载均可.    a) http://www.eclipse.org/downloads/eclipse-packages/          ...

  4. requirejs使用

    实现模块化.按需加载. 一.配置参数. 1.baseUrl:以一个相对于baseUrl的地址来加载所有的代码.   data-main,使用它来启动脚本加载过程,而baseUrl一般设置到与该属性相一 ...

  5. Week 1:2015/4/27~2015/5/3

    Update everyday.(Last edit:4/30 01:00) Task 1:TPO X 2.5(finish 1,then finish 2 more) Task 2:TC Tarja ...

  6. Scala的trait

    一:说明 1.介绍 2.功能 二:具体解释功能 1.定义接口 2.定义方法 3.定义字段 4.定义抽象字段 5.混合trait

  7. CMake undefined reference to `QTcpServer::QTcpServer(QObject*)'的解决

    1. 这是因为工程link不到network的库,因此除了要包含头文件    #include 之外,还需要在.pro文件中加入:    QT += network 2. 对于CMake,需添加Net ...

  8. flex布局

    一,啥是flex? 1.Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性.任何一个容器都可以指定为Flex布局. .box{ displa ...

  9. Android下安装应用不成功解决

    在手机上安装应用程序不成功,可以尝试把手机连接电脑,然后使用adb进行安装,adb安装命令: adb install Android_65632.apk 当出现: success!就表示成功,但当不成 ...

  10. Java jdbc 连接oracle之二(使用properties文件)

    driver = oracle.jdbc.driver.OracleDriver url = jdbc:oracle:thin:@192.168.10.105:1521:orcl user = LF ...