Spring Resource接口

Resource

Resource继承结构

EncodedResource (org.springframework.core.io.support)
Resource (org.springframework.core.io)
    AbstractResource (org.springframework.core.io)
        DescriptiveResource (org.springframework.core.io)
        BeanDefinitionResource (org.springframework.beans.factory.support)
        GzippedResource in GzipResourceResolver (org.springframework.web.servlet.resource)
        FileSystemResource (org.springframework.core.io)
        MultipartFileResource (org.springframework.web.multipart)
        ByteArrayResource (org.springframework.core.io)
        PathResource (org.springframework.core.io)
        AbstractFileResolvingResource (org.springframework.core.io)
            UrlResource (org.springframework.core.io)
                FileUrlResource (org.springframework.core.io)
            ClassPathResource (org.springframework.core.io)
                ClassPathContextResource in DefaultResourceLoader (org.springframework.core.io)
        FileNameVersionedResource in VersionResourceResolver (org.springframework.web.reactive.resource)
        FileNameVersionedResource in VersionResourceResolver (org.springframework.web.servlet.resource)
        InputStreamResource (org.springframework.core.io)
        EncodedResource in EncodedResourceResolver (org.springframework.web.servlet.resource)
        VfsResource (org.springframework.core.io)

FileSystemResource: 对 java.io.File 类型资源的封装.
ByteArrayResource: 会根据字节数组的数据构造一个相应的 ByteArrayInputStream.
UrlResource: 对 java.net.URL类型资源的封装.
ClassPathResource: 类路径资源的封装.

AbstractResource

默认实现的方法,如:
contentLength() : 通过byte[]数组读取一遍文件,统计文件大小.
lastModified() : 返回文件最后修改时间

ResourceLoader

Spring Resource接口_第1张图片

ResourceLoader
    DefaultResourceLoader (org.springframework.core.io)
        AbstractApplicationContext (org.springframework.context.support)
        ClassRelativeResourceLoader (org.springframework.core.io)
        FileSystemResourceLoader (org.springframework.core.io)
        ServletContextResourceLoader (org.springframework.web.context.support)
    ResourcePatternResolver (org.springframework.core.io.support)
        PathMatchingResourcePatternResolver (org.springframework.core.io.support)
            ServletContextResourcePatternResolver (org.springframework.web.context.support)
        ApplicationContext (org.springframework.context)
public interface ResourceLoader {
    String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;
    // 获取资源
    Resource getResource(String location);
    // 获取classLoader
    ClassLoader getClassLoader();
}

DefaultResourceLoader.getResource() 提供了默认的资源加载策略.

ResourcePatternResolver

Resource[] getResources(String locationPattern) throws IOException;

在 ResourceLoader 的基础上增加了 getResources(String locationPattern),支持根据路径匹配返回多个 Resource 实例

PathMatchingResourcePatternResolver

它除了支持 ResourceLoader 和 ResourcePatternResolver 新增的 classpath*: 前缀外,还支持 Ant 风格的路径匹配模式(比如 **/*.java)

你可能感兴趣的:(Spring)