精华内容
参与话题
问答
  • 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

     

    展开全文
  • Use ResourceLoader

    2020-12-09 02:34:22
    <div><p>Loading js and css files directly from the output handler has been deprecated since MediaWiki 1.17 with the introduction of ResourceLoader. <p>Consider using it so that this skin will make ...
  • 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();
        }
    
    }
    展开全文
  • ResourceLoader 获取资源

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

    resource.png

    ResourceLoader是为了屏蔽了Resource的具体实现,统一资源的获取方式。你即能从ResourceLoader加载ClassPathResource,也能加载FileSystemResource等
    public interface ResourceLoader {
    // 默认从类路径加载的资源 前缀: “classpath:”,获取ClassPathResource
    String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;
    Resource getResource(String location);
    ResourceLoader接口默认对classpath路径下面的资源进行加载
    public interface ResourcePatternResolver extends ResourceLoader {
    // 默认加载所有路径(包括jar包)下面的文件,“classpath*:”, 获取ClassPathResource
    String CLASSPATH_ALL_URL_PREFIX = “classpath*:”;
    ResourcePatternResolver默认会加载所有路径下面的文件,获得ClassPathResource;classpath:只会在class类路径下查找;而classpath*:会扫描所有JAR包及class类路径下出现的文件
    //Ant风格表达式 com/smart//*.xml
    ResourcePatternResoler resolver = new PathMatchingResourcePatternResolver();
    Resource resources[] = resolver.getResources("com/smart/
    /*.xml");

    // ApplicationContext ctx
    //FileSystemResource资源
    Resource template = ctx.getResource(“file:///res.txt”);
    //UrlResource资源
    Resource template = ctx.getResource(“https://my.cn/res.txt”);
    ResourceLoader方法getResource的locationPattern可设置资源模式前缀来获取非ClassPathResource资源,locationPattern支持Ant风格
    前缀 示例 描述 classpath: classpath:config.xml 从类路径加载 file: file:///res.txt 从文件系统加载FileSystemResource http: http://my.cn/res.txt 加载UrlResource

    展开全文
  • Spring之ResourceLoader

    2019-12-30 22:11:41
    Spring之ResourceLoader Spring中的ResourceLoader用于加载资源。可以从文件中、网络中、流中加载资源。 资源加载器是个接口,默认从类路径下加载。通过getResource方法从字符串中查找路径。可以从容器中获取...

    Spring之ResourceLoader

    Spring中的ResourceLoader用于加载资源。可以从文件中、网络中、流中加载资源。

    资源加载器是个接口,默认从类路径下加载。通过getResource方法从字符串中查找路径。可以从容器中获取ResourceLoader,调用getResource方法。

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

    applicationContext为容器:获取类路径下的资源

            Resource resource = applicationContext.getResource("demo.txt");
    		String filename = resource.getFilename();
            System.out.println(filename);
            InputStream inputStream = resource.getInputStream();
            byte[] b=new byte[1024];
    
            int read = inputStream.read(b);
            System.out.println(new String(b,0,read));
    
    
            Resource resource1 = applicationContext.getResource("ApplicationContext.xml");
            String filename1 = resource1.getFilename();
            System.out.println(filename1);
    
            String description = resource1.getDescription();
            System.out.println(description);
    

    applicationContext为容器:获取网络资源

    		Resource resource2 = applicationContext.getResource("http://jsonplaceholder.typicode.com/posts");
            InputStream inputStream1 = resource2.getInputStream();		
    
    		byte[] b2=new byte[10240];
            BufferedInputStream stream = new BufferedInputStream(inputStream1);
            int i = stream.read(b2);
            System.out.println(new String(b2,0,i));
            stream.close();
    
    展开全文
  • 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:// ...
  • 主要介绍了Spring实战之ResourceLoader接口资源加载用法,结合实例形式分析了Spring使用ResourceLoader接口加载资源的相关配置与使用技巧,需要的朋友可以参考下
  • resourceLoader' variable, not a local variable. This fix initializes the static UIContext.resourceLoader variable that is then used in numerous locations to load map reosurce files. <p>Resolves: ...
  • <div><p>The resourceLoader is the Mediawiki sub-system which allow to load, per article, javascript/css dependences. The documentation is here: https://www.mediawiki.org/wiki/ResourceLoader</p> <p>...
  • 1.spring中使用ResourceLoader来加载资源 2.ResourcePatternResolver ResourcePatternResolver中的getResources()方法返回多个Resource资源,参数支持“classpath*”和通配符路径和文件名称。 3....
  • <p>No ResourceLoader implementation has been provided. "file:///Users/.../css/home.css" No ResourceLoader implementation has been provided. Can't read the url "file:///Users/.../views...
  • s default ResourceLoader so that it can use all of the application's currently populated resource loaders?</p><p>该提问来源于开源项目:spring-projects/spring-amqp</p></div>
  • 统一资源加载ResourceLoader spring分开了资源和资源加载,结下来就看看资源加载,还是先来张类结构图,整体看看,类和接口来自spring core包。 ResourceLoader接口 ResourceLoader接口是spring对资源加载的抽象。...
  • Spring使用ResourceLoader接口获取资源

    万次阅读 2018-08-12 16:10:30
    前言 Spring中整合了获取资源的工具,就是使用Resource接口。 有关Resource接口的相关... ResourceLoader接口 而Spring框架为了更方便的获取资源,尽量弱化程序员对各个Resource接口的实现类的感知,定义了另...
  • ResourceLoader接口 spring应用手册(第五部分) 上一章节,我们解释了Resource接口。现在我们来看看ResourceLoader。 Spring框架为了更方便的获取资源,尽量弱化程序员对各个Resource接口实现类的感知与分辨...
  • ResourceLoader Spring的ApplicationContext继承了ResourceLoader接口.这个接口主要就是可以加载各种resource.. 接口还是比较简单的: 1 /* 2 * Copyright 2002-2014 the original author or authors. 3 ...
  • 一、ResourceLoader继承实现结构 二、DefaultResourceLoader 主要是实现单个资源文件的加载 三、ResourcePatternResolver主要是显示多个资源文件的加载
  • if (ResourceLoader.CanProvideContentFor(new ResourceLoader.ResourceLoadingQuery { AssemblyName = typeof(BindingLabel).GetTypeInfo().Assembly.GetName(), ResourcePath = "Views/...
  • 引入ResourceLoader所属的Jar包 &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-core&lt;/artifactId&gt; &lt;...
  • 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...
  • Webx框架:ResourceLoader

    千次阅读 2014-11-27 21:59:05
    Spring中的ResourceLoader 获取Resource的方法有两种。一种是通过ResourceLoader载入资源,另外一种是通过注入。通过ResourceLoader载入资源的方法如下: public class Test implements ResourceLoaderAware { ...
  • <p><code>ResourceLoader</code> could be quite useful for JavaScript Connectors that might want to dynamically load external scripts. The class is however only accessible using GWT right now. <p>...
  • Spring中资源的加载是定义在ResourceLoader接口中的,它跟前面提到的抽象资源的关系如下: ResourceLoader的源码 public interface ResourceLoader { /** Pseudo URL prefix for loading from the class path: ...

空空如也

1 2 3 4 5 ... 20
收藏数 1,126
精华内容 450
关键字:

resourceloader