spring常用的4種加載資源的前綴

1.  ‘classpath:’ 類路徑中加載資源,可以加載所有classpath目錄所包含的資源

		Resource fileClasspath = applicationContext.getResource("classpath:test.txt");
		this.outResource(fileClasspath);


2. ‘file:’ 文件系統中加載資源,可以加載所有文件系統中有權限訪問的資源

		Resource fileResource = applicationContext.getResource("file:C:/tmp/test.txt");
		this.outResource(fileResource);

3. ‘http:’/‘ftp:’  http/ftp加載資源,可以加載互聯網上的資源


		Resource fileUrl = applicationContext.getResource("http://git.oschina.net/notifications/count");
		this.outResource(fileUrl);

4. 不加前綴,此時實際加載分爲兩種情況

(1),在web項目中,可以加載應用上下文的所有資源

(2),非web項目中,等同於‘classpath:’,但是類加載器和‘classpath:’不完全相同

		Resource fileProject = applicationContext.getResource("test.txt");
		this.outResource(fileProject);



完整示例:

 
	ApplicationContext applicationContext;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
		this.outResource("file:C:/tmp/test.txt");
		this.outResource("classpath:classpath.txt");
		this.outResource("webapp.txt");
		this.outResource("http://127.0.0.1:80/test.txt");
	}

	private void outResource(String resStr) {
		Resource resource = applicationContext.getResource(resStr);
		InputStream stream = null;
		try {
			System.err.println("");
			System.err.println("加載的資源:"+resStr);
			System.err.println("包裝資源的類:" + resource.getClass().getName());
			System.err.println("包裝資源的訪問地址:" + resource.getURI());
			stream = resource.getInputStream();
			System.err.println("讀取到的內容:" + StreamUtils.copyToString(stream, Charset.defaultCharset()));
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (stream != null)
				try {
					stream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

非web項目下輸出

加載的資源:file:C:/tmp/test.txt
包裝資源的類:org.springframework.core.io.UrlResource
包裝資源的訪問地址:file:C:/tmp/test.txt
讀取到的內容:加載FileSystem文件

加載的資源:classpath:classpath.txt
包裝資源的類:org.springframework.core.io.ClassPathResource
包裝資源的訪問地址:file:/C:/git-repo/tm/target/classes/classpath.txt
讀取到的內容:加載classpath資源

加載的資源:webapp.txt
包裝資源的類:org.springframework.core.io.DefaultResourceLoader$ClassPathContextResource
包裝資源的訪問地址:file:/C:/git-repo/tm/target/classes/webapp.txt
讀取到的內容:加載ServletContext資源

加載的資源:http://127.0.0.1:80/test.txt
包裝資源的類:org.springframework.core.io.UrlResource
包裝資源的訪問地址:http://127.0.0.1:80/test.txt
讀取到的內容:加載nginx服務中的資源


web項目下輸出

加載的資源:file:C:/tmp/test.txt
包裝資源的類:org.springframework.core.io.UrlResource
包裝資源的訪問地址:file:C:/tmp/test.txt
讀取到的內容:加載FileSystem文件

加載的資源:classpath:classpath.txt
包裝資源的類:org.springframework.core.io.ClassPathResource
包裝資源的訪問地址:file:/C:/git-repo/tm/target/classes/classpath.txt
讀取到的內容:加載classpath資源

加載的資源:webapp.txt
包裝資源的類:org.springframework.web.context.support.ServletContextResource
包裝資源的訪問地址:file:/C:/git-repo/tm/src/main/webapp/webapp.txt
讀取到的內容:加載ServletContext資源

加載的資源:http://127.0.0.1:80/test.txt
包裝資源的類:org.springframework.core.io.UrlResource
包裝資源的訪問地址:http://127.0.0.1:80/test.txt
讀取到的內容:加載nginx服務中的資源
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章