精华内容
下载资源
问答
  • ResourceLoader

    2020-08-18 23:44:54
    ResourceLoader的作用是对资源封装的加载器。 String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX; 指定类根路径前缀字符串 classPath Resource getResource(String location) //获取指定路径下...

    ResourceLoader的作用是对资源封装的加载器。

    String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX; 指定类根路径前缀字符串 classPath

    Resource getResource(String location)  //获取指定路径下的资源

    ClassLoader getClassLoader(); //获取类加载器

    DefaultResourceLoader 默认资源加载器

    org.springframework.core.io.ProtocolResolver

    优先执行指定的协议加载器,去加载资源;

    协议加载器加载不到,判断地址路径是否是/ 根路径,是则从项目的上下文根路径,返回下述对象org.springframework.core.io.DefaultResourceLoader.ClassPathContextResource

    如果不是项目根路径则判断是否有classPath的前缀,有的话直接返回

    org.springframework.core.io.ClassPathResource

    如果路径也不是ClassPath前缀的话,则直接将路径作为URL对象进行封装。

    org.springframework.core.io.UrlResource

     

    展开全文
  • ResourceLoader使用

    2021-05-29 17:44:43
    public class ResourceLoaderTest { ... DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource("test.properties"); try { .
    public class ResourceLoaderTest {
        public static void main(String[] args) {
            DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
            Resource resource = resourceLoader.getResource("test.properties");
            try {
                File file = resource.getFile();
                boolean file1 = file.isFile();
                FileInputStream fis = new FileInputStream(file);
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(resource);
            Resource resource1 =  resourceLoader.getResource("D:\\code");
            if(resource1 instanceof ClassPathResource){
                System.out.println(resource1+"存在于classpathresource");
            }
            Resource resource2 =  resourceLoader.getResource("file:D:\\code");
            if(resource2 instanceof UrlResource){
                System.out.println("ur2");
                try {
                    File file = resource2.getFile();
                    boolean directory = file.isDirectory();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Resource resource3 = resourceLoader.getResource("http://www.baidu.com");
            if(resource3 instanceof UrlResource){
                System.out.println("baidu");
            }
        }
    }
    展开全文
  • Spring ResourceLoader

    2018-05-30 11:16:28
    1.ResourceLoader 资源加载器接口 public interface ResourceLoader { //从类路径加载资源前缀 String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX; //返回一个要处理的资源对象 Resource get...

    1.ResourceLoader 资源加载器接口

    public interface ResourceLoader {
        //从类路径加载资源前缀
        String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;
        //返回一个要处理的资源对象
        Resource getResource(String location);
        //暴露出一个类加载器
        ClassLoader getClassLoader();
    }

    2.ProtocolResolver 协议解析接口

    public interface ProtocolResolver {
    
        //根据指定位置解析和resouceLoader返回一个Resouce对象
        Resource resolve(String location, ResourceLoader resourceLoader);
    
    }

    3.InputStreamSource 输入源

    public interface InputStreamSource {
    
        //Return An InputStream
        InputStream getInputStream() throws IOException;
    }

    4.Resource 资源

    public interface Resource extends InputStreamSource {
    
        /**
         * Return whether this resource actually exists in physical form.
         * <p>This method performs a definitive existence check, whereas the
         * existence of a {@code Resource} handle only guarantees a
         * valid descriptor handle.
         */
        boolean exists();
    
        /**
         * Return whether the contents of this resource can be read,
         * e.g. via {@link #getInputStream()} or {@link #getFile()}.
         * <p>Will be {@code true} for typical resource descriptors;
         * note that actual content reading may still fail when attempted.
         * However, a value of {@code false} is a definitive indication
         * that the resource content cannot be read.
         * @see #getInputStream()
         */
        boolean isReadable();
    
        /**
         * Return whether this resource represents a handle with an open
         * stream. If true, the InputStream cannot be read multiple times,
         * and must be read and closed to avoid resource leaks.
         * <p>Will be {@code false} for typical resource descriptors.
         */
        boolean isOpen();
    
        /**
         * Return a URL handle for this resource.
         * @throws IOException if the resource cannot be resolved as URL,
         * i.e. if the resource is not available as descriptor
         */
        URL getURL() throws IOException;
    
        /**
         * Return a URI handle for this resource.
         * @throws IOException if the resource cannot be resolved as URI,
         * i.e. if the resource is not available as descriptor
         */
        URI getURI() throws IOException;
    
        /**
         * Return a File handle for this resource.
         * @throws IOException if the resource cannot be resolved as absolute
         * file path, i.e. if the resource is not available in a file system
         */
        File getFile() throws IOException;
    
        /**
         * Determine the content length for this resource.
         * @throws IOException if the resource cannot be resolved
         * (in the file system or as some other known physical resource type)
         */
        long contentLength() throws IOException;
    
        /**
         * Determine the last-modified timestamp for this resource.
         * @throws IOException if the resource cannot be resolved
         * (in the file system or as some other known physical resource type)
         */
        long lastModified() throws IOException;
    
        /**
         * Create a resource relative to this resource.
         * @param relativePath the relative path (relative to this resource)
         * @return the resource handle for the relative resource
         * @throws IOException if the relative resource cannot be determined
         */
        Resource createRelative(String relativePath) throws IOException;
    
        /**
         * Determine a filename for this resource, i.e. typically the last
         * part of the path: for example, "myfile.txt".
         * <p>Returns {@code null} if this type of resource does not
         * have a filename.
         */
        String getFilename();
    
        /**
         * Return a description for this resource,
         * to be used for error output when working with the resource.
         * <p>Implementations are also encouraged to return this value
         * from their {@code toString} method.
         * @see Object#toString()
         */
        String getDescription();

    5.AbstractResource 抽象类 对Resource的实现

    public abstract class AbstractResource implements Resource {
        /**
         * This implementation checks whether a File can be opened,
         * falling back to whether an InputStream can be opened.
         * This will cover both directories and content resources.
         */
        @Override
        public boolean exists() {
            // Try file existence: can we find the file in the file system?
            try {
                return getFile().exists();
            }
            catch (IOException ex) {
                // Fall back to stream existence: can we open the stream?
                try {
                    InputStream is = getInputStream();
                    is.close();
                    return true;
                }
                catch (Throwable isEx) {
                    return false;
                }
            }
        }
    
        /**
         * This implementation always returns {@code true}.
         */
        @Override
        public boolean isReadable() {
            return true;
        }
    
        /**
         * This implementation always returns {@code false}.
         */
        @Override
        public boolean isOpen() {
            return false;
        }
    
        /**
         * This implementation throws a FileNotFoundException, assuming
         * that the resource cannot be resolved to a URL.
         */
        @Override
        public URL getURL() throws IOException {
            throw new FileNotFoundException(getDescription() + " cannot be resolved to URL");
        }
    
        /**
         * This implementation builds a URI based on the URL returned
         * by {@link #getURL()}.
         */
        @Override
        public URI getURI() throws IOException {
            URL url = getURL();
            try {
                return ResourceUtils.toURI(url);
            }
            catch (URISyntaxException ex) {
                throw new NestedIOException("Invalid URI [" + url + "]", ex);
            }
        }
    
        /**
         * This implementation throws a FileNotFoundException, assuming
         * that the resource cannot be resolved to an absolute file path.
         */
        @Override
        public File getFile() throws IOException {
            throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");
        }
    
        /**
         * This implementation reads the entire InputStream to calculate the
         * content length. Subclasses will almost always be able to provide
         * a more optimal version of this, e.g. checking a File length.
         * @see #getInputStream()
         * @throws IllegalStateException if {@link #getInputStream()} returns null.
         */
        @Override
        public long contentLength() throws IOException {
            InputStream is = getInputStream();
            Assert.state(is != null, "Resource InputStream must not be null");
            try {
                long size = 0;
                byte[] buf = new byte[255];
                int read;
                while ((read = is.read(buf)) != -1) {
                    size += read;
                }
                return size;
            }
            finally {
                try {
                    is.close();
                }
                catch (IOException ex) {
                }
            }
        }
    
        /**
         * This implementation checks the timestamp of the underlying File,
         * if available.
         * @see #getFileForLastModifiedCheck()
         */
        @Override
        public long lastModified() throws IOException {
            long lastModified = getFileForLastModifiedCheck().lastModified();
            if (lastModified == 0L) {
                throw new FileNotFoundException(getDescription() +
                        " cannot be resolved in the file system for resolving its last-modified timestamp");
            }
            return lastModified;
        }
    
        /**
         * Determine the File to use for timestamp checking.
         * <p>The default implementation delegates to {@link #getFile()}.
         * @return the File to use for timestamp checking (never {@code null})
         * @throws IOException if the resource cannot be resolved as absolute
         * file path, i.e. if the resource is not available in a file system
         */
        protected File getFileForLastModifiedCheck() throws IOException {
            return getFile();
        }
    
        /**
         * This implementation throws a FileNotFoundException, assuming
         * that relative resources cannot be created for this resource.
         */
        @Override
        public Resource createRelative(String relativePath) throws IOException {
            throw new FileNotFoundException("Cannot create a relative resource for " + getDescription());
        }
    
        /**
         * This implementation always returns {@code null},
         * assuming that this resource type does not have a filename.
         */
        @Override
        public String getFilename() {
            return null;
        }
    
    
        /**
         * This implementation returns the description of this resource.
         * @see #getDescription()
         */
        @Override
        public String toString() {
            return getDescription();
        }
    
        /**
         * This implementation compares description strings.
         * @see #getDescription()
         */
        @Override
        public boolean equals(Object obj) {
            return (obj == this ||
                (obj instanceof Resource && ((Resource) obj).getDescription().equals(getDescription())));
        }
    
        /**
         * This implementation returns the description's hash code.
         * @see #getDescription()
         */
        @Override
        public int hashCode() {
            return getDescription().hashCode();
        }
    
    }
    展开全文
  • resorce 3.0 ResourceLoader

    2021-07-23 21:56:02
    ResourceLoader ResourceLoader 接口由能返回(或者载入)Resource 实例的对象来实现。 public interface ResourceLoader { Resource getResource(String location); } 所有的application context都实现了 ...

    ResourceLoader

    ResourceLoader 接口由能返回(或者载入)Resource 实例的对象来实现。

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

    所有的application context都实现了 ResourceLoader 接口, 因此它们可以用来获取Resource 实例。

    当你调用特定application context的 getResource() 方法, 而且资源路径并没有特定的前缀时,你将获得与该application context相应的 Resource 类型。例如:假定下面的代码片断是基于ClassPathXmlApplicationContext 实例上执行的:

    Resource template = ctx.getResource("some/resource/path/myTemplate.txt");
    

    这将返回ClassPathResource;如果是基于FileSystemXmlApplicationContext 实例上执行的,那你将获得FileSystemResource。而对于 WebApplicationContext 你将获得ServletContextResource,依此类推。

    这样你可以在特定的application context中用流行的方法载入资源。

    另一方面,无论什么类型的application context, 你可以通过使用特定的前缀 classpath: 强制使用ClassPathResource。

    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");
    

    在这里插入图片描述

    展开全文
  • ResourceLoader 获取资源

    2020-11-12 08:13:53
    ResourceLoader是为了屏蔽了Resource的具体实现,统一资源的获取方式。你即能从ResourceLoader加载ClassPathResource,也能加载FileSystemResource等 public interface ResourceLoader { // 默认从类路径加载的资源 ...
  • Spring之ResourceLoader

    2019-12-30 22:11:41
    Spring之ResourceLoader Spring中的ResourceLoader用于加载资源。可以从文件中、网络中、流中加载资源。 资源加载器是个接口,默认从类路径下加载。通过getResource方法从字符串中查找路径。可以从容器中获取...
  • 主要介绍了Spring实战之ResourceLoader接口资源加载用法,结合实例形式分析了Spring使用ResourceLoader接口加载资源的相关配置与使用技巧,需要的朋友可以参考下
  • Spring ResourceLoader接口

    2019-04-10 10:51:11
    ResourceLoader接口的主要作用是进行org.springframework.core.io.ResourceLoader对象实例化,这个接口的定义如下: 读取指定的资源信息:public getResource Resource getResource(String location); 取得类加载器...
  • ResourceLoader源码如下: public interface ResourceLoader {  String CLASSPATH_URL_PREFIX = "classpath:";  Resource getResource(String var1);  @Nullable  ClassLoader getClassLoader(); } ...
  • 15、使用ResourceLoader

    千次阅读 2016-05-22 17:52:59
    ResourceLoader接口类如下:public interface ResourceLoader { Resource getResource(String location); }ResourceLoader根据资源的前缀判断是什么类型的资源 classpath: ClassPathResource file:// ...
  • Webpack插件,用于将ResourceLoader前端模块自动添加到extension.json 。 它会提取国际化标签和需要mobileFrontend模块, mw.mobileFrontend.require 。 npm install --save-dev update-resourceloader-config-...
  • Spring使用ResourceLoader接口获取资源

    万次阅读 2018-08-12 16:10:30
    前言 Spring中整合了获取资源的工具,就是使用Resource接口。 有关Resource接口的相关... ResourceLoader接口 而Spring框架为了更方便的获取资源,尽量弱化程序员对各个Resource接口的实现类的感知,定义了另...
  • 一、ResourceLoader继承实现结构 二、DefaultResourceLoader 主要是实现单个资源文件的加载 三、ResourcePatternResolver主要是显示多个资源文件的加载
  • 引入ResourceLoader所属的Jar包 &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-core&lt;/artifactId&gt; &lt;...
  • Spring中资源的加载是定义在ResourceLoader接口中的,它跟前面提到的抽象资源的关系如下: ResourceLoader的源码 public interface ResourceLoader { /** Pseudo URL prefix for loading from the class path: ...
  • 1.spring中使用ResourceLoader来加载资源 2.ResourcePatternResolver ResourcePatternResolver中的getResources()方法返回多个Resource资源,参数支持“classpath*”和通配符路径和文件名称。 3....
  • 统一资源加载ResourceLoader spring分开了资源和资源加载,结下来就看看资源加载,还是先来张类结构图,整体看看,类和接口来自spring core包。 ResourceLoader接口 ResourceLoader接口是spring对资源加载的抽象。...
  • ResourceLoader接口 spring应用手册(第五部分) 上一章节,我们解释了Resource接口。现在我们来看看ResourceLoader。 Spring框架为了更方便的获取资源,尽量弱化程序员对各个Resource接口实现类的感知与分辨...
  • ResourceLoader Spring的ApplicationContext继承了ResourceLoader接口.这个接口主要就是可以加载各种resource.. 接口还是比较简单的: 1 /* 2 * Copyright 2002-2014 the original author or authors. 3 ...
  • Webx框架:ResourceLoader

    千次阅读 2014-11-27 21:59:05
    Spring中的ResourceLoader 获取Resource的方法有两种。一种是通过ResourceLoader载入资源,另外一种是通过注入。通过ResourceLoader载入资源的方法如下: public class Test implements ResourceLoaderAware { ...
  • ResourceLoader must not be null

    千次阅读 2020-02-17 11:13:21
    在SSM框架开发中,使用Junit4测试...java.lang.IllegalArgumentException: ResourceLoader must not be null at org.springframework.util.Assert.notNull(Assert.java:115) at org.springframework.core.io...
  • 【Spring源码分析】03-ResourceLoader

    千次阅读 2018-05-09 20:41:24
    ResourceLoader 接口是用来加载 Resource 对象的,换句话说,就是当一个对象需要获取 Resource 实例时,可以选择实现 ResourceLoader 接口。public interface ResourceLoader { /** Pseudo URL prefix for loading ...
  • (十二)ResourceLoader接口

    2018-05-29 14:23:09
    ResourceLoader接口的主要作用是进行org.springframework.core.io.ResourceLoader对象实例化,这个接口的定义如下: 读取指定的资源信息:public getResource Resource getResource(String location); 取得类加载器:...
  • Spring.ResourceLoader

    2014-06-23 17:51:41
    ResourceLoader接口的默认实现,通过参数资源路径的前缀获得相应的Resource. 1、若以"classpath:"开头,则获得classPathResource 2、若location符合URL格式,如http:,ftp:,file:,jar等,则获得...
  • 这一节总结一下资源加载起ResourceLoader。 所谓资源加载器,就是把上一节讲到的Resource加载到spring中用到的工具类,在spring中被设计为ResourceLoader。 简单的看下ResourceLoader接口 /** * Strategy ...
  • spring resourceLoader 读取配置文件

    千次阅读 2015-08-28 11:34:43
    spring resourceLoader 读取配置文件

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 17,502
精华内容 7,000
关键字:

resourceloader