forked from DerekYRC/mini-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
229 additions
and
69 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
src/main/java/org/springframework/core/io/AbstractResource.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/org/springframework/core/io/DefaultResourceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
src/main/java/org/springframework/core/io/FileSystemResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/main/java/org/springframework/core/io/ResourceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/main/java/org/springframework/core/io/UrlResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/org/springframework/test/ioc/ResourceAndResourceLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello world |