zoukankan      html  css  js  c++  java
  • Spring ClassPathResource

    Spring ClassPathResource

    ClassPathResource用于加载资源文件,如果类路径资源文件位于文件系统中,支持解析为File,但是不用于JAR中的资源。

    org.springframework.core.io.ClassPathResource位于Spring核心core下,用以表达类路径下的资源 。

    其继承实现关系如下图:

    ClasspathResource类的主要属性变量和构造方法如下

    //资源文件路径
    private final String path;
    //通过类加载器加载资源
    @Nullable
    private ClassLoader classLoader;
    //通过Class类加载资源文件
    @Nullable
    private Class<?> clazz;
    

    通过资源路径和classLoader创建ClassPathResource对象,classLoader默认为null

    public ClassPathResource(String path, @Nullable 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());
    }
    

    关于StringUtils,可以参考: Spring中的SpringUtils

    
    // 通过类路径和给定的Class类创建ClassPathResource对象
    public ClassPathResource(String path, @Nullable Class<?> clazz) {
    	Assert.notNull(path, "Path must not be null");
        //规范化资源文件路径
    	this.path = StringUtils.cleanPath(path);
    	this.clazz = clazz;
    }
    
    
    

    getInputStream()方法,为给定的类路径资源打开一个InputStream

    public InputStream getInputStream() throws IOException {
    		InputStream is;
    		//判断clazz对象是否为null,不为null的话,获取InputStream对象
    		if (this.clazz != null) {
    			is = this.clazz.getResourceAsStream(this.path);
    		}
    		//判断classLoader对象是否为null,不为null的话,获取InputStream对象
    		else if (this.classLoader != null) {
    			is = this.classLoader.getResourceAsStream(this.path);
    		}
    		//获取InputStream对象
    		else {
    			is = ClassLoader.getSystemResourceAsStream(this.path);
    		}
    		//抛出异常
    		if (is == null) {
    			throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");
    		}
    		return is;
    	}
    

    getURL():返回底层类路径资源的URL

    @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;
    }
    

    如下为测试代码:

    
            //Resource resource=new ClassPathResource("resource/conf.txt",Thread.currentThread().getContextClassLoader());
    
           // Resource resource=new ClassPathResource("resource/conf.txt",ResourceMain.class.getClassLoader());
    
            Resource resource=new ClassPathResource("resource/conf.txt");
    
            InputStream inputStream = resource.getInputStream();
            ByteArrayOutputStream bts=new ByteArrayOutputStream();
            int i;
            while ((i=inputStream.read())!=-1){
                bts.write(i);
            }
    
            System.out.println(bts.toString());
            System.out.println(resource);
            System.out.println(resource.getURI());
            System.out.println(resource.getURL());
            System.out.println(resource.getDescription());
            System.out.println(resource.getFile());
            System.out.println(resource.getFilename());
    

    相关源码参考: github

  • 相关阅读:
    偷窃转基因玉米种子引发中美打农业官司
    关于PreferenceActivity的使用和一些问题的解决(自己定义Title和取值)
    大写中文数字-財务
    【leetcode】LRU
    【AC大牛陈鸿的ACM总结贴】【ID AekdyCoin】人家当初也一样是菜鸟
    android面试题 不单单为了面试也是一次非常好的学习
    存储系统的实现-探析存储的机制和原理
    unity3d脚本编程
    ubuntu12.04 安装配置jdk1.7
    Android中一个类实现的接口数不能超过七个
  • 原文地址:https://www.cnblogs.com/haizhilangzi/p/12717368.html
Copyright © 2011-2022 走看看