• spring(四):Resource


    Resource

    Spring的Resource接口代表底层外部资源,提供了对底层外部资源的一致性访问接口。

    public interface Resource extends InputStreamSource {
        boolean exists();
        default boolean isReadable() { return this.exists(); }
        default boolean isOpen() { return false; }
        default boolean isFile() { return false; }
        URL getURL() throws IOException;
        URI getURI() throws IOException;
        File getFile() throws IOException;
        default ReadableByteChannel readableChannel() throws IOException {
            return Channels.newChannel(this.getInputStream());
        }
        long contentLength() throws IOException;
        long lastModified() throws IOException;
        Resource createRelative(String var1) throws IOException;
        @Nullable
        String getFilename();
        String getDescription();
    }
    

    Resource接口提供了足够的抽象,足够满足我们日常使用。而且提供了很多内置Resource实现:ByteArrayResource、InputStreamResource 、FileSystemResource 、UrlResource 、ClassPathResource、ServletContextResource、VfsResource等。

    ByteArrayResource

    	private final byte[] byteArray;
        private final String description;
    
    	return new ByteArrayInputStream(this.byteArray);
    

    InputStreamResource

        private final InputStream inputStream;
        private final String description;
        private boolean read;
    

    FileSystemResource

        private final String path;
        @Nullable
        private final File file;
        private final Path filePath;
        
        return Files.newInputStream(this.filePath);
    

    UrlResource

        @Nullable
        private final URI uri;
        private final URL url;
        private final URL cleanedUrl;
        
        URLConnection con = this.url.openConnection();
        return con.getInputStream();
    

    ClassPathResource

        private final String path;
        @Nullable
        private ClassLoader classLoader;
        @Nullable
        private Class<?> clazz;
    
    	if (this.clazz != null) {
            is = this.clazz.getResourceAsStream(this.path);
        } else if (this.classLoader != null) {
            is = this.classLoader.getResourceAsStream(this.path);
        } else {
            is = ClassLoader.getSystemResourceAsStream(this.path);
        }
    

    ServletContextResource

    代表web应用资源

        private final ServletContext servletContext;
        private final String path;
        
        InputStream is = this.servletContext.getResourceAsStream(this.path);
    

    VfsResource

    代表虚拟文件系统资源

        private final Object resource;
        
        return VfsUtils.getInputStream(this.resource);
    

    ResourceLoader接口

    public interface ResourceLoader {
        String CLASSPATH_URL_PREFIX = "classpath:";
        Resource getResource(String var1);
        @Nullable
        ClassLoader getClassLoader();
    }
    

    对于目前所有ApplicationContext都实现了ResourceLoader,因此可以使用其来加载资源。

    ClassPathXmlApplicationContext:不指定前缀将返回默认的ClassPathResource资源,否则将根据前缀来加载资源;

        // 第一类构造器是根据提供的配置文件路径使用“ResourcePatternResolver”
    	// 的“getResources()”接口通过匹配获取资源;即如“classpath:config.xml”
        public ClassPathXmlApplicationContext(String configLocation)...
        public ClassPathXmlApplicationContext(String... configLocations)...
        public ClassPathXmlApplicationContext(String[] configLocations, @Nullable ApplicationContext parent)...
        public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh)...
        public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)...
    
    	// 第二类构造器是根据提供的路径和clazz来构造ClassResource资源。
        // 即采用“public ClassPathResource(String path, Class<?> clazz)”构造器获取资源。
        public ClassPathXmlApplicationContext(String path, Class<?> clazz)...
        public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz)...
        public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, @Nullable ApplicationContext parent)...
    

    FileSystemXmlApplicationContext:不指定前缀将返回FileSystemResource,否则将根据前缀来加载资源;

    	// 将加载相对于当前工作目录的“configLocation”位置的资源
    	// 在linux系统上不管“configLocation”是否带“/”,都作为相对路径
    	// 在window系统上如“D:/resourceInject.xml”是绝对路径。
    	// 因此在除非很必要的情况下,不建议使用该ApplicationContext。
        public FileSystemXmlApplicationContext(String configLocation)...
        public FileSystemXmlApplicationContext(String... configLocations)...
        public FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent)...
        public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh)...
        public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)...
    

    WebApplicationContext:不指定前缀将返回ServletContextResource,否则将根据前缀来加载资源;

    其他:不指定前缀根据当前上下文返回Resource实现,否则将根据前缀来加载资源。

  • 相关阅读:
    主从复制(多台服务器搭建环境)【十五】
    Linux中kill,pkill,killall和xkill命令汇总讲解
    redis持久化机制【十三】
    守护进程详解及创建,daemon()使用
    【small项目】MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
    Redis事务【十二】
    乐观锁和悲观锁的区别
    Redis 命令与连接【十一】
    Redis 配置【十】
    DB_Links创建际删除
  • 原文地址:https://www.cnblogs.com/angelica-duhurica/p/11188092.html
Copyright © 2020-2023  润新知