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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM