美文网首页Spring FrameWork Core Tech
The IoC Container 2. Resources

The IoC Container 2. Resources

作者: 小鲍比大爷 | 来源:发表于2019-03-01 09:12 被阅读0次

    2. Resources

    2.1. Introduction

    阐述java.net.URL的种种缺点,说明其不足以用来描述所有资源。

    2.2. The Resource Interface

    针对资源访问,Spring定义了自己的接口:

    public interface Resource extends InputStreamSource {
    
        boolean exists();
    
        boolean isOpen();
    
        URL getURL() throws IOException;
    
        File getFile() throws IOException;
    
        Resource createRelative(String relativePath) throws IOException;
    
        String getFilename();
    
        String getDescription();
    
    }
    
    public interface InputStreamSource {
    
        InputStream getInputStream() throws IOException;
    
    }
    

    接口相关重要方法说明:

    • getInputStream(): Locates and opens the resource, returning an InputStream for reading from the resource. It is expected that each invocation returns a fresh InputStream. It is the responsibility of the caller to close the stream.
    • exists(): Returns a boolean indicating whether this resource actually exists in physical form.
    • isOpen(): Returns a boolean indicating whether this resource represents a handle with an open stream. If true, the InputStream cannot be read multiple times and must be read once only and then closed to avoid resource leaks. Returns false for all usual resource implementations, with the exception of InputStreamResource.
    • getDescription(): Returns a description for this resource, to be used for error output when working with the resource. This is often the fully qualified file name or the actual URL of the resource.

    Spring在很多情况下都用了Resource接口。即使用户不使用Spring框架,Spring也非常推荐Resource接口及其相关实现作为java的资源访问的替代方案,虽然这样跟Spring产生了耦合,但是用起来相当于引入了第三方库,没有什么负面影响。

    2.3. Built-in Resource Implementations

    Spring包含以下Resource的默认实现:

    2.4. The ResourceLoader

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

    所有application contexts都实现了ResourceLoader接口,所以可以通过自动装配的将ResourceLoader直接注入到bean中使用。同时,application contexts都具有getResource()方法,可以直接调用。ApplicationContext的getResource()方法的返回类型为2.3中提到的几种类型。有两种方式可以决定返回的Resource类型:1,资源前缀classpath或者file等;2,在没有资源前缀的情况下,由ApplicationContext类型决定,比如ClassPathXmlApplicationContext,在不指定资源前缀的情况下,返回ClassPathResource类型。

    Resource template = ctx.getResource("some/resource/path/myTemplate.txt");
    Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");
    Resource template = ctx.getResource("file:///some/resource/path/myTemplate.txt");
    Resource template = ctx.getResource("http://myhost.com/resource/path/myTemplate.txt");
    

    2.5. The ResourceLoaderAware interface

    可以通过继承ResourceLoaderAware接口,由Spring注入ResourceLoader。不过推荐直接使用@Autowired,更方便。

    public interface ResourceLoaderAware {
    
        void setResourceLoader(ResourceLoader resourceLoader);
    }
    

    2.6. Resources as Dependencies

    可以直接注入Resource类型,Spring会根据前缀情况自动转换相应的Resource类型:

    @Data
    @Component
    public class ResourceBean {
    
        @Autowired
        @Value("classpath:app.properties")
        private Resource resource;
    
        @Autowired
        @Value("file:///app.properties")
        private Resource resource1;
    }
    
    

    2.7. Application Contexts and Resource Paths

    通配符的使用,兼容性问题和一些使用注意事项。建议直接看官方文档。

    相关文章

      网友评论

        本文标题:The IoC Container 2. Resources

        本文链接:https://www.haomeiwen.com/subject/nfbnyqtx.html