Spring Resource、ResourceLoader、ResourcePatternResolver学习
自己的理解:
1.Resource是包装资源的对象,该接口继承inputstreamSource接口是为继承getinputstream方法,打开一个输入流,Resource对象可以是ClassPathResource、FileSystemResource、UrlResource等
2.ResourceLoader接口就是通过getresource方法来new各种resource对象
3.如果需要按某种匹配方式来new resource对象,所以出现ResourcePatternResolver接口,继承ResourceLoad,增加匹配功能
4.ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
在ClassPathXmlApplicationContext的构造方法中调用了AbstractApplicationContext的构造方法
在AbstractApplicationContext里创建了一个
private ResourcePatternResolver resourcePatternResolver=new PathMatchingResourcePatternResolver()
5.AbstractApplicationContext继承了DefaultResourceLoader类
6.PathMatchingResourcePatternResolver的流程如下
Resource类图
ResouurceLoad类图
在bean中获取Resource的方式
通过上面内容的介绍,我们知道,在bean中获取Resource主要有以下几种方式:
1.直接通过new各种类型的Resource来获取对应的Resource。
2.在bean里面获取到对应的ApplicationContext,再通过ApplicationContext的getResource(String path)方法获取对应的Resource。
3.直接创建DefaultResourceLoader的实例,再调用其getResource(String location)方法获取对应的Resource。
4.通过依赖注入的方式把Resource注入到bean中。示例如下:
在Spring内部,针对于资源文件有一个统一的接口Resource表示。其主要实现类有ClassPathResource、FileSystemResource、UrlResource、ByteArrayResource、ServletContextResource和InputStreamResource。Resource接口中主要定义有以下方法:
public class ResourceTest {
/**
* ClassPathResource可以用来获取类路径下的资源
* @throws IOException
*/
@Test
public void testClassPath() throws IOException {
Resource resource = new ClassPathResource("test.txt");
String fileName = resource.getFilename();
System.out.println(fileName);
// resource.getFile(); //获取资源对应的文件
// resource.getURL(); //获取资源对应的URL
if (resource.isReadable()) {
//每次都会打开一个新的流
InputStream is = resource.getInputStream();
this.printContent(is);
}
}
/**
* FileSystemResource可以用来获取文件系统里面的资源,对于FileSystemResource而言我们
* 可以获取到其对应的输出流。
* @throws IOException
*/
@Test
public void testFileSystem() throws IOException {
FileSystemResource resource = new FileSystemResource("D:\\test.txt");
if (resource.isReadable()) {
//FileInputStream
printContent(resource.getInputStream());
}
if (resource.isWritable()) {
//每次都会获取到一个新的输出流
OutputStream os = resource.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
bw.write("你好,中国!");
bw.flush();
if (os != null) {
os.close();
}
if (bw != null) {
bw.close();
}
}
}
/**
* 针对于URL进行封装的Resource,可用来从URL获取资源内容
* @throws Exception
*/
@Test
public void testURL() throws Exception {
UrlResource resource = new UrlResource("http://www.google.com.hk");
if (resource.isReadable()) {
//URLConnection对应的getInputStream()。
printContent(resource.getInputStream());
}
}
/**
* 针对于字节数组封装的Resource,用来从字节数组获取资源内容
* @throws IOException
*/
@Test
public void testByteArray() throws IOException {
ByteArrayResource resource = new ByteArrayResource("Hello".getBytes());
//ByteArrayInputStream()
printContent(resource.getInputStream());
}
/**
* 针对于输入流的Resource,其getInputStream()方法只能被调用一次。
* @throws Exception
*/
@Test
public void testInputStream() throws Exception {
InputStream is = new FileInputStream("D:\\test.txt");
InputStreamResource resource = new InputStreamResource(is);
//对于InputStreamResource而言,其getInputStream()方法只能调用一次,继续调用将抛出异常。
InputStream target = resource.getInputStream(); //返回的就是构件时的那个InputStream
//is将在printContent方法里面进行关闭
printContent(target);
}
/**
* 输出输入流的内容
* @param is
* @throws IOException
*/
private void printContent(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line=br.readLine()) != null) {
System.out.println(line);
}
if (is != null) {
is.close();
}
if (br != null) {
br.close();
}
}
}
2 通过ResourceLoader获取资源
在Spring里面还定义有一个ResourceLoader接口,该接口中只定义了一个用于获取Resource的getResource(String location)方法。它的实现类有很多,这里我们先挑一个DefaultResourceLoader来讲。DefaultResourceLoader在获取Resource时采用的是这样的策略:首先判断指定的location是否含有“classpath:”前缀,如果有则把location去掉“classpath:”前缀返回对应的ClassPathResource;否则就把它当做一个URL来处理,封装成一个UrlResource进行返回;如果当成URL处理也失败的话就把location对应的资源当成是一个ClassPathResource进行返回。
@Test
public void testResourceLoader() {
ResourceLoader loader = new DefaultResourceLoader();
Resource resource = loader.getResource("http://www.google.com.hk");
System.out.println(resource instanceof UrlResource); //true
//注意这里前缀不能使用“classpath*:”,这样不能真正访问到对应的资源,exists()返回false
resource = loader.getResource("classpath:test.txt");
System.out.println(resource instanceof ClassPathResource); //true
resource = loader.getResource("test.txt");
System.out.println(resource instanceof ClassPathResource); //true
}
ApplicationContext接口也继承了ResourceLoader接口,所以它的所有实现类都实现了ResourceLoader接口,都可以用来获取Resource。
对于ClassPathXmlApplicationContext而言,它在获取Resource时继承的是它的父类DefaultResourceLoader的策略。
FileSystemXmlApplicationContext也继承了DefaultResourceLoader,但是它重写了DefaultResourceLoader的getResourceByPath(String path)方法。所以它在获取资源文件时首先也是判断指定的location是否包含“classpath:”前缀,如果包含,则把location中“classpath:”前缀后的资源从类路径下获取出来,当做一个ClassPathResource;否则,继续尝试把location封装成一个URL,返回对应的UrlResource;如果还是失败,则把location指定位置的资源当做一个FileSystemResource进行返回。