参考文献

Java标准资源管理

  • Java标准资源定位

    职责 说明
    面向资源 文件系统,artifact(jar,war,ear文件)以及远程资源(HTTP,FTP等)
    API整合 java.lang.ClassLoader#getResource,java.io.Filejava.net.URL
    资源定位 java.net.URLjava.net.URI
    面向流式存储 java.net.URLConnection
    协议扩展 java.net.URLStreamHandlerjava.net.URLStreamHandlerFactory
  • Java URL 协议扩展

    • 基于java.net.URLStreamHandlerFactory

      • 基于 java.net.URLStreamHandlerFactory 扩展协议
    • 基于java.net.URLStreamHandler

      • JDK 1.8内建协议实现

        协议 实现类
        file sun.net.www.protocol.file.Handler
        ftp sun.net.www.protocol.ftp.Handler
        http sun.net.www.protocol.http.Handler
        https sun.net.www.protocol.https.Handler
        jar sun.net.www.protocol.jar.Handler
        mailto sun.net.www.protocol.mailto.Handler
        netdoc sun.net.www.protocol.netdoc.Handler
      • 实现类名必须为Handler

        实现类命名规则 说明
        默认 sun.net.www.protocol.${protocol}.Handler
        自定义 通过Java Properties java.protocol.handler.pkgs指定实现类包名,实现类名必须为Handler.如果存在多包名指定,通过分隔符"|"

    Spring 资源接口

    类型 接口
    输入流 org.springframework.core.io.InputStreamSource
    只读资源 org.springframework.core.io.Resource
    可写资源 org.springframework.core.io.WritableResource
    编码资源 org.springframework.core.io.support.EncodeResource
    上下文资源 org.springframework.core.io.ContextResource

    Spring 内建Resource实现

    资源来源 资源协议 实现类
    Bean定义 org.springframework.beans.factory.support.BeanDefintionResource
    数组 org.springframework.core.io.ByteArrayResource
    类路径 classpath:/ org.springframework.core.io.ClassPathResource
    文件系统 file:/ org.springframework.core.io.FileSystemResource
    URL URL支持的协议 org.springframework.core.io.UrlResource
    ServletContex org.springframwork.web.context.support.ServletContextResource

Spring Resource接口扩展

  • 可写资源接口

    • org.springframework.core.io.WritableResource
      • org.springframework.core.io.FileSystemResource
      • org.springframework.core.io.FileUrlResource(@Since 5.0.2)
      • org.springframework.core.io.PathResource(@Since 4.0 & @Deprecated)
  • 编码资源接口

    • org.springframework.core.io.support.EncodedResource

Spring 资源加载器

  • Resource加载器
    • org.springframework.core.io.ResourceLoader
      • org.springframework.core.io.DefaultResourceLoader
        • org.springframework.core.io.FileSystemResourceLoader
        • org.springframework.core.io.ClassRelativeResourceLoader
        • org.springframework.core.io.support.AbstractApplicationContext
  • 通配路径ResourceLoader
    • org.springframework.core.io.support.ResourcePatternResolver
      • org.springframework.core.io.support.PathMatchingResourcePatternResolve
  • 路径匹配器
    • org.springframework.util.PathMatcher
      • Ant模式匹配实现: org.springframework.util.AntPathMatcher

Spring 通配路径资源扩展

  • 实现org.springframework.util.PathMatcher
  • 重置PathMatcher
    • PathMatchingResourcePatternResolve#setPathMatcher

依赖注入Spring Resource

  • 基于@Value实现

    1
    2
    @Value("classpath:/...")
    prvate Resource resource;

依赖注入ResourceLoader

  • 方法一: 实现ResourceLoaderAware回调
  • 方法二: @Autowired注入ResourceLoader
  • 方法三: 注入ApplicationContextResourceLoader