© 版權聲明:本文為博主原創文章,轉載請注明出處
Resources
針對於資源文件的統一接口
-UrlResource:URL對應的資源,根據一個URL地址即可創建
-ClassPathResource:獲取類路徑下的資源文件
-FileSystemResource:獲取文件系統里面的資源
-ServletContextResource:ServletContext封裝的資源,用於訪問ServletContext環境下的資源(需導入spring-web.jar,需在web環境中測試,本次不測試。)
-InputStreamResource:針對於輸入流封裝的資源
-ByteArrayResource:針對於字節數組封裝的資源
ResourceLoader
spring的IOC容器中,所有的Application context都實現了ResourceLoader這個接口,所有的application context都可以用getResource()方法來獲取Resource實例。
前綴:
classpath:從類路徑中加載
file:從文件系統中作為url加載
http:作為url加載
(none):依賴於application context,也就是說application context是怎么加載的,它就是怎么加載的
實例
1.項目結構

2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springresources</groupId>
<artifactId>Spring-Resources</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-Resources Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 統一開發所需版本 -->
<properties>
<spring.version>4.3.7.RELEASE</spring.version>
</properties>
<dependencies>
<!-- junit依賴 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring核心依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>Spring-Resources</finalName>
</build>
</project>
3.test-resources.txt
測試spring Resource
4.SpringResource.java
package org.spring.resources;
import java.io.IOException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
public class SpringResource implements ApplicationContextAware {
private ApplicationContext context;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
/**
* 通過classpath前綴獲取Resource
*
* @throws IOException
*/
public void getClassPathResouce() throws IOException {
Resource resource = context.getResource("classpath:test-resources.txt");
System.out.println("ResourceLoader(classpath) filename: " + resource.getFilename());
System.out.println("ResourceLoader(classpath) size: " + resource.contentLength());
}
/**
* 通過file前綴獲取Resource
*
* @throws IOException
*/
public void getFileResouce() throws IOException {
Resource resource = context.getResource("file:E:\\code\\Spring-Resources\\src\\main\\resources\\test-resources.txt");
System.out.println("ResourceLoader(file) filename: " + resource.getFilename());
System.out.println("ResourceLoader(file) size: " + resource.contentLength());
}
/**
* 通過url前綴獲取Resource
*
* @throws IOException
*/
public void getUrlResouce() throws IOException {
Resource resource = context.getResource("url:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/");
System.out.println("ResourceLoader(url) filename: " + resource.getFilename());
System.out.println("ResourceLoader(url) size: " + resource.contentLength());
}
/**
* 無前綴獲取Resource
*
* 跟application context有關,本代碼中的application context是根據classpath獲取的,所以該方法中也是根據classpath前綴獲取的Resource
*
* @throws IOException
*/
public void getResouce() throws IOException {
Resource resource = context.getResource("test-resources.txt");
System.out.println("ResourceLoader(none) filename: " + resource.getFilename());
System.out.println("ResourceLoader(none) size: " + resource.contentLength());
}
}
5.spring-resources.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="springResource" class="org.spring.resources.SpringResource"/>
</beans>
6.JunitTestBase.java
package org.spring.resources.test;
import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;
/**
* Junit基類
*
*/
public class JunitTestBase {
private ClassPathXmlApplicationContext context;
private String springXmlPath;
/**
* 無參構造器,使用此構造器則使用默認spring配置文件
*/
public JunitTestBase() {
}
/**
* 含參構造器,初始化spring配置文件路徑
*
* @param springXmlPath
* spring配置文件路徑
*/
public JunitTestBase(String springXmlPath) {
super();
this.springXmlPath = springXmlPath;
}
/**
* 加載spring配置文件到容器中,並啟動容器
* 在@Test方法執行之前執行
*/
@Before
public void before() {
if(StringUtils.isEmpty(springXmlPath)) {//默認spring配置文件
springXmlPath = "classpath:spring-*.xml";
}
//加載spring配置文件到spring容器中
context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
//啟動spring容器,並將啟動信號擴散器該容器下的所有組件中
context.start();
}
/**
* 銷毀Spring容器
* 在@Test方法執行之后執行
*/
@After
public void after() {
if(context != null){
context.destroy();
}
}
/**
* 通過bean Id獲取對象
*
* @param beanId
* bean id
* @return
*/
public Object getBean(String beanId) {
return context.getBean(beanId);
}
}
7.TestSpringResources.java
package org.spring.resources.test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.spring.resources.SpringResource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.UrlResource;
@RunWith(BlockJUnit4ClassRunner.class)//默認執行類,可不寫
public class TestSpringResources extends JunitTestBase {
//構造器初始化spring配置文件
public TestSpringResources() {
super("classpath:spring-resources.xml");
}
/**
* 測試UrlResource
*
* @throws IOException
*/
@Test
public void testUrlResources() throws IOException {
UrlResource resource = new UrlResource("http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/");
System.out.println("UrlResource filename:" + resource.getFilename());
System.out.println("UrlResource size:" + resource.contentLength());
}
/**
* 測試ClassPathResource
*
* @throws IOException
*/
@Test
public void testClassPathResource() throws IOException {
ClassPathResource resource = new ClassPathResource("test-resources.txt");
System.out.println("ClassPathResource filename:" + resource.getFilename());
System.out.println("ClassPathResource size:" + resource.contentLength());
}
/**
* 測試FileSystemResource
*
* @throws IOException
*/
@Test
public void testFileSystemResource() throws IOException {
FileSystemResource resource = new FileSystemResource("E:\\code\\Spring-Resources\\src\\main\\resources\\test-resources.txt");
System.out.println("FileSystemResource filename:" + resource.getFilename());
System.out.println("FileSystemResource size:" + resource.contentLength());
}
/**
* 測試InputStreamResource
*
* @throws IOException
*/
@Test
public void testInputStreamResource() throws IOException {
InputStream is = new FileInputStream("E:\\code\\Spring-Resources\\src\\main\\resources\\test-resources.txt");
InputStreamResource resource = new InputStreamResource(is);
System.out.println("InputStreamResource filename:" + resource.getFilename());
System.out.println("InputStreamResource size:" + resource.contentLength());
}
/**
* 測試ByteArrayResource
*
* @throws IOException
*/
@Test
public void testByteArrayResource() throws IOException {
ByteArrayResource resource = new ByteArrayResource("Test Resource".getBytes());
System.out.println("ByteArrayResource filename:" + resource.getFilename());
System.out.println("ByteArrayResource size:" + resource.contentLength());
}
/**
* 測試ResourceLoader-classpath前綴
*
* @throws IOException
*/
@Test
public void testResourceLoaderClassPath() throws IOException {
SpringResource springResource = (SpringResource) super.getBean("springResource");
springResource.getClassPathResouce();
}
/**
* 測試ResourceLoader-file前綴
*
* @throws IOException
*/
@Test
public void testResourceLoaderFile() throws IOException {
SpringResource springResource = (SpringResource) super.getBean("springResource");
springResource.getFileResouce();
}
/**
* 測試ResourceLoader-url前綴
*
* @throws IOException
*/
@Test
public void testResourceLoaderUrl() throws IOException {
SpringResource springResource = (SpringResource) super.getBean("springResource");
springResource.getUrlResouce();
}
/**
* 測試ResourceLoader-無前綴
*
* @throws IOException
*/
@Test
public void testResourceLoader() throws IOException {
SpringResource springResource = (SpringResource) super.getBean("springResource");
springResource.getResouce();
}
}
8.效果預覽
8.1 執行testUrlResources方法

8.2 執行testClassPathResource方法 
8.3 執行testFileSystemResource方法

8.4 執行testInputStreamResource方法

8.5 執行testByteArrayResource方法

8.6 執行testResourceLoaderClassPath方法

8.7 執行testResourceLoaderFile方法

8.8 執行testResourceLoaderUrl方法

8.9 執行testResourceLoader方法

參考:http://www.imooc.com/video/3758
