zoukankan      html  css  js  c++  java
  • Spring ClassPathResource加载资源文件详解

    先看Demo

    @Test
    public void testClassPathResource() throws IOException {
        Resource res = new ClassPathResource("resource/ApplicationContext.xml");
        InputStream input = res.getInputStream();
        Assert.assertNotNull(input);
    }

    再看内部源码

    public ClassPathResource(String path) {
         this(path, (ClassLoader) null);
    }
    
    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());
    }
    
    public ClassPathResource(String path, Class<?> clazz) {
         Assert.notNull(path, "Path must not be null");
         this.path = StringUtils.cleanPath(path);
         this.clazz = clazz;
    }

    获取资源内容

    /**
     * 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;
    }

    源码解读

    该类获取资源的方式有两种:Class获取和ClassLoader获取。

    @Test
    public void testResouce() {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        System.out.println(loader.getResource("").getPath());
        System.out.println(this.getClass().getResource("").getPath());
        System.out.println(this.getClass().getResource("/").getPath());
        System.out.println(System.getProperty("user.dir"));
    }

    运行结果:

    /home/temp/spring_test/target/test-classes/
    /home/temp/spring_test/target/test-classes/com/test/spring/spring_test/
    /home/temp/spring_test/target/test-classes/
    /home/temp/spring_test
    //获取的是相对于当前类的相对路径
    Class.getResource("")
    //获取的是classpath的根路径
    Class.getResource("/")
    //获取的是classpath的根路径
    ClassLoader.getResource("")

    总结

    在创建ClassPathResource对象时,我们可以指定是按Class的相对路径获取文件还是按ClassLoader来获取。

  • 相关阅读:
    U3D不同平台载入XML文件的方法——IOS MAC Android
    C++使用规范小记
    设置角色对象可见性
    编辑器菜单操作
    U3D资源动态加载异步方案探究
    Animation动画
    Unity3D失去焦点时继续渲染
    C#打开当前目录
    组件模式代码实践(C#版本)
    Unity3D批处理脚本
  • 原文地址:https://www.cnblogs.com/rinack/p/14957773.html
Copyright © 2011-2022 走看看