Skip to content

Commit

Permalink
资源和资源加载器
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekYRC committed Nov 25, 2020
1 parent 63d2ef7 commit 5f79b1c
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 69 deletions.
Binary file added assets/resource.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 72 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,75 @@ public class PopulateBeanWithPropertyValuesTest {
}
```

## 资源和资源加载器
## 资源和资源加载器
Resource是资源的抽象和访问接口,简单写了三个实现类

![](./assets/resource.png)

- FileSystemResource,文件系统资源的实现类
- ClassPathResource,classpath下资源的实现类
- UrlResource,对java.net.URL进行资源定位的实现类

ResourceLoader接口则是资源查找定位策略的抽象,DefaultResourceLoader是其默认实现类

测试:
```
public class ResourceAndResourceLoaderTest {
@Test
public void testResourceLoader() throws Exception {
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
//加载classpath下的资源
Resource resource = resourceLoader.getResource("classpath:hello.txt");
InputStream inputStream = resource.getInputStream();
String content = IoUtil.readUtf8(inputStream);
System.out.println(content);
assertThat(content).isEqualTo("hello world");
//加载文件系统资源
resource = resourceLoader.getResource("src/test/resources/hello.txt");
assertThat(resource instanceof FileSystemResource).isTrue();
inputStream = resource.getInputStream();
content = IoUtil.readUtf8(inputStream);
System.out.println(content);
assertThat(content).isEqualTo("hello world");
//加载url资源
resource = resourceLoader.getResource("https://www.baidu.com");
assertThat(resource instanceof UrlResource).isTrue();
inputStream = resource.getInputStream();
content = IoUtil.readUtf8(inputStream);
System.out.println(content);
}
}
```





























39 changes: 0 additions & 39 deletions src/main/java/org/springframework/core/io/AbstractResource.java

This file was deleted.

25 changes: 4 additions & 21 deletions src/main/java/org/springframework/core/io/ClassPathResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,27 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
* 类路径下的资源
* classpath下的资源
*
* @author derekyi
* @date 2020/11/25
*/
public class ClassPathResource extends AbstractResource {
public class ClassPathResource implements Resource {

private final String path;

private ClassLoader classLoader;

public ClassPathResource(String path) {
this.path = path;
this.classLoader = ClassPathResource.class.getClassLoader();
}

@Override
public InputStream getInputStream() throws IOException {
InputStream is = this.classLoader.getResourceAsStream(this.path);
InputStream is = this.getClass().getClassLoader().getResourceAsStream(this.path);
if (is == null) {
throw new FileNotFoundException("resource cannot be opened because it does not exist");
throw new FileNotFoundException(this.path + " cannot be opened because it does not exist");
}
return is;
}

@Override
public URL getURL() throws IOException {
URL url = this.classLoader.getResource(this.path);
if (url == null) {
throw new FileNotFoundException("resource cannot be resolved to URL because it does not exist");
}
return url;
}

public String getPath() {
return path;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.springframework.core.io;

import java.net.MalformedURLException;
import java.net.URL;

/**
* @author derekyi
* @date 2020/11/25
*/
public class DefaultResourceLoader implements ResourceLoader {

public static final String CLASSPATH_URL_PREFIX = "classpath:";

@Override
public Resource getResource(String location) {
if (location.startsWith(CLASSPATH_URL_PREFIX)) {
//classpath下的资源
return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
} else {
try {
//尝试当成url来处理
URL url = new URL(location);
return new UrlResource(url);
} catch (MalformedURLException ex) {
//当成文件系统下的资源处理
String path = location;
if (location.startsWith("/")) {
path = location.substring(1);
}
return new FileSystemResource(location);
}
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/org/springframework/core/io/FileSystemResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.springframework.core.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;

/**
* @author derekyi
* @date 2020/11/25
*/
public class FileSystemResource implements Resource {

private final String filePath;

public FileSystemResource(String filePath) {
this.filePath = filePath;
}

@Override
public InputStream getInputStream() throws IOException {

try {
Path path = new File(this.filePath).toPath();
return Files.newInputStream(path);
} catch (NoSuchFileException ex) {
throw new FileNotFoundException(ex.getMessage());
}
}
}
9 changes: 1 addition & 8 deletions src/main/java/org/springframework/core/io/Resource.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package org.springframework.core.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
* 资源的抽象接口
* 资源的抽象和访问接口
*
* @author derekyi
* @date 2020/11/25
*/
public interface Resource {

boolean exists();

File getFile() throws IOException;

InputStream getInputStream() throws IOException;

URL getURL() throws IOException;
}
12 changes: 12 additions & 0 deletions src/main/java/org/springframework/core/io/ResourceLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.springframework.core.io;

/**
* 资源加载器接口
*
* @author derekyi
* @date 2020/11/25
*/
public interface ResourceLoader {

Resource getResource(String location);
}
29 changes: 29 additions & 0 deletions src/main/java/org/springframework/core/io/UrlResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.springframework.core.io;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
* @author derekyi
* @date 2020/11/25
*/
public class UrlResource implements Resource {

private final URL url;

public UrlResource(URL url) {
this.url = url;
}

@Override
public InputStream getInputStream() throws IOException {
URLConnection con = this.url.openConnection();
try {
return con.getInputStream();
} catch (IOException ex) {
throw ex;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.springframework.test.ioc;

import cn.hutool.core.io.IoUtil;
import org.junit.Test;
import org.springframework.core.io.*;

import java.io.InputStream;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author derekyi
* @date 2020/11/25
*/
public class ResourceAndResourceLoaderTest {

@Test
public void testResourceLoader() throws Exception {
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();

//加载classpath下的资源
Resource resource = resourceLoader.getResource("classpath:hello.txt");
InputStream inputStream = resource.getInputStream();
String content = IoUtil.readUtf8(inputStream);
System.out.println(content);
assertThat(content).isEqualTo("hello world");

//加载文件系统资源
resource = resourceLoader.getResource("src/test/resources/hello.txt");
assertThat(resource instanceof FileSystemResource).isTrue();
inputStream = resource.getInputStream();
content = IoUtil.readUtf8(inputStream);
System.out.println(content);
assertThat(content).isEqualTo("hello world");

//加载url资源
resource = resourceLoader.getResource("https://www.baidu.com");
assertThat(resource instanceof UrlResource).isTrue();
inputStream = resource.getInputStream();
content = IoUtil.readUtf8(inputStream);
System.out.println(content);
}
}
1 change: 1 addition & 0 deletions src/test/resources/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world

0 comments on commit 5f79b1c

Please sign in to comment.