zoukankan      html  css  js  c++  java
  • 阅读之spring框架

    资源表示:Resource
    Spring框架内部使用org.springframework.core.io.Resouce接口作为所有资源的抽象和访问接口。

    它继承了 org.springframework.core.io.InputStreamSource接口。作为所有资源的统一抽象,Source 定义了一些通用的方法,由子类 AbstractResource 提供统一的默认实现。定义如下:

    public interface Resource extends InputStreamSource {

        /**

         * 资源是否存在

         */

        boolean exists();

        /**

         * 资源是否可读

         */

        default boolean isReadable() {

            return true;

        }

        /**

         * 资源所代表的句柄是否被一个stream打开了

         */

        default boolean isOpen() {

            return false;

        }

        /**

         * 是否为 File

         */

        default boolean isFile() {

            return false;

        }

        /**

         * 返回资源的URL的句柄

         */

        URL getURL() throws IOException;

        /**

         * 返回资源的URI的句柄

         */

        URI getURI() throws IOException;

        /**

         * 返回资源的File的句柄

         */

        File getFile() throws IOException;

        /**

         * 返回 ReadableByteChannel

         */

        default ReadableByteChannel readableChannel() throws IOException {

            return Channels.newChannel(getInputStream());

        }

        /**

         * 资源内容的长度

         */

        long contentLength() throws IOException;

        /**

         * 资源最后的修改时间

         */

        long lastModified() throws IOException;

        /**

         * 根据资源的相对路径创建新资源

         */

        Resource createRelative(String relativePath) throws IOException;

        /**

         * 资源的文件名

         */

        @Nullable

        String getFilename();

        /**

         * 资源的描述

         */

        String getDescription();

    }

    Resouce接口可以根据资源的不同类型,或者资源位置的不同,给出对应的具体实现,Spring框架提供了一些实现类:

    ByteArrayResource。将字节数组作为资源进行封装,如果通过InputStream形式访问该类型的资源,该实现会根据字节数组的数据,构造出对应的ByteArrayInputStream并返回。

    ClassPathResource。从Java应用的classpath中加载具体的资源并封装,可以使用指定的类加载器或者给定的类进行资源加载。

    FileSystemResource。对 java.io.File 类型资源的封装,只要是跟 File 打交道的,基本上与 FileSystemResource 也可以打交道。支持文件和 URL 的形式,实现 WritableResource 接口,且从 Spring Framework 5.0 开始,FileSystemResource 使用NIO.2 API进行读/写交互

    UrlResource。通过java.net.URL进行具体资源查找定位的实现类。

    InputStreamResource。将给定的InputStream视为一种资源的Resource实现类。



    资源加载:ResourceLoader

    在Spring框架中,ResourceLoader是资源查找定位策略的统一抽象,具体的资源查找定位策略由相应的ResourceLoader实现类给出。

    org.springframework.core.io.ResourceLoader 为 Spring 资源加载的统一抽象,具体的资源加载则由相应的实现类来完成,所以我们将 ResourceLoader 称作为统一资源定位器。定义如下:

    public interface ResourceLoader {

        String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;

        Resource getResource(String location);

        ClassLoader getClassLoader();

    }

    ResourceLoader 接口提供两个方法:getResource()、getClassLoader()。

    getResource()根据所提供资源的路径 location 返回 Resource 实例,但是它不确保该 Resource 一定存在,需要调用 Resource.exist()方法判断。该方法支持以下模式的资源加载:

    URL位置资源,如”file:C:/test.dat”

    ClassPath位置资源,如”classpath:test.dat”

    相对路径资源,如”WEB-INF/test.dat”,此时返回的Resource实例根据实现不同而不同

    getClassLoader() 返回 ClassLoader 实例,对于想要获取 ResourceLoader 使用的 ClassLoader 用户来说,可以直接调用该方法来获取,

    对于想要获取 ResourceLoader 使用的 ClassLoader 用户来说,可以直接调用 getClassLoader() 方法获得。在分析 Resource 时,提到了一个类 ClassPathResource ,这个类是可以根据指定的 ClassLoader 来加载资源的。

  • 相关阅读:
    自动打包脚本
    Tomcat内存溢出问题
    Nginx笔记总结二十:nginx索引目录配置
    Nginx笔记总结十九:nginx + fancy实现漂亮的索引目录
    Nginx笔记总结十八:nginx统计响应的http状态码信息(ngx-http-status-code-counter)
    Nginx笔记总结十七:nginx生成缩略图配置(http_image_filter_module)
    Nginx笔记总结十六:nginx优化指南
    Nginx笔记总结十五:nginx+keepalive+proxy_cache配置高可用nginx集群和高速缓存
    Nginx笔记总结十四: nginx反向代理,用内网域名转发
    Nginx笔记总结十三:nginx 正向代理
  • 原文地址:https://www.cnblogs.com/liulala2017/p/10583811.html
Copyright © 2011-2022 走看看