[源码系列:手写spring] IOC第六节:资源和资源加载器

主要内容

[源码系列:手写spring] IOC第六节:资源和资源加载器_第1张图片

本节新增

  • Resource接口 定义对资源的抽象和访问,并且添加三个Resource接口的简单实现类

  • FileSystemResource 文件系统资源的实现类

  • ClassPathResource classpath下资源的实现类

  • UrlResource 对java.net.URL进行资源定位的实现类

  • ResourceLoader接口 资源加载器接口

  • DefaultResourceLoader 加载器实现类

代码分支

resource-and-resource-loader

核心代码

Resource

public interface Resource {
    InputStream getInputStream() throws IOException;
}

ResourceLoader

public interface ResourceLoader {
    Resource getResource(String location);
}

ClassPathResource

public class ClassPathResource implements Resource{
    private final String path;

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

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

    }
}

FileSystemResource

public class FileSystemResource implements Resource{
    private final String filePath;

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

    @Override public InputStream getInputStream() throws IOException{
            Path path = new File(this.filePath).toPath();
            return  Files.newInputStream(path);
    }
}

UrlResource

public class UrlResource implements Resource{
    private final URL url;

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

    @Override public InputStream getInputStream() throws IOException {
        URLConnection urlConnection = this.url.openConnection();
        return urlConnection.getInputStream();
    }
}

DefaultResourceLoader

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)){
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
        }else {
            try {
                URL url = new URL(location);
                return new UrlResource(url);
            }catch (Exception e){
                return new FileSystemResource(location);
            }

        }
    }
}

测试

@Test
    public void test() throws IOException {
        DefaultResourceLoader loader = new DefaultResourceLoader();
        //classpath下资源的加载
        Resource resource = loader.getResource("classpath:test.txt");
        InputStream inputStream = resource.getInputStream();
        String content =  IoUtil.readUtf8(inputStream);
        System.out.println("classpath - 资源内容测试:"+content);

        //文件系统资源的加载
        resource = loader.getResource("src/test/resources/test.txt");
        inputStream = resource.getInputStream();
        content =  IoUtil.readUtf8(inputStream);
        System.out.println("文件系统 - 资源内容测试:"+content);
        //url资源的加载
        resource = loader.getResource("https://blog.csdn.net/weixin_43848166");
        inputStream = resource.getInputStream();
        content =  IoUtil.readUtf8(inputStream);
        System.out.println("URL - 资源内容测试:"+content);
    }

测试结果

classpath - 资源内容测试:hello-word
文件系统 - 资源内容测试:hello-word
URL - 资源内容测试:一辉ComeOn的博客_CSDN博客-Spring源码剖析,每日算法,MySQL进阶领域博主
... 

你可能感兴趣的:(Spring源码剖析,spring,java,后端)