Spring 配置资源Resource

Java中,不同来源的资源抽象成URL,通过注册不同的handler(URLStreamHandler)来处理不同来源的资源的读取逻辑。一般handler的类型使用不同的前缀(协议,protocal)来识别,如:“file:”、“http:“、”jar:”等。

对于Spring,URL没有定义相应的,如“classpath:“的handler,定义也相对麻烦,Spring对配置文件的读取做了相应的封装,通过Resource接口来抽象底层资源。如下:

/**
 * Interface for a resource descriptor that abstracts from the actual
 * type of underlying resource, such as a file or class path resource.
 *
 * <p>An InputStream can be opened for every resource if it exists in
 * physical form, but a URL or File handle can just be returned for
 * certain resources. The actual behavior is implementation-specific.
 *
 * @author Juergen Hoeller
 * @since 28.12.2003
 * @see #getInputStream()
 * @see #getURL()
 * @see #getURI()
 * @see #getFile()
 * @see WritableResource
 * @see ContextResource
 * @see FileSystemResource
 * @see ClassPathResource
 * @see UrlResource
 * @see ByteArrayResource
 * @see InputStreamResource
 * @see PathResource
 */
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();

}

对于不同来源的资源文件都有相应的Resource实现,文件(FileSystemResource)、Classpath资源(ClassPathResource)、URL资源(UrlResource)、InputStream资源(InputStreamResource)、Byte数组资源(ByteArrayResource)等。

时间: 2024-08-02 06:58:04

Spring 配置资源Resource的相关文章

spring 配置资源路径时候,classpath:/,classpath:,不带前缀的区别

/** * spring 配置资源路径时候,classpath:/,classpath:,不带前缀的区别, * 其实没区别,spring 规定 "classpath:" pseudo-URL,伪url路径,在处理这种路径前缀 * 时候,会把这个伪url去掉. * @author doctor * * @time 2014年12月2日 下午6:28:12 */ public class DefaultResourceLoaderPractice { @Test public void t

Spring讲解-----------资源(resource)

4.1.1  概述 在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理URL资源.File资源资源.ClassPath相关资源.服务器相关资源(JBoss AS 5.x上的VFS资源)等等很多资源.因此处理这些资源需要使用不同的接口,这就增加了我们系统的复杂性:而且处理这些资源步骤都是类似的(打开资源.读取资源.关闭资源),因此如果能抽象出一个统一的接口来对这些底层资源进行统一访问,是不是很方便,而且使我们系统更加简洁,都是对不同的底层资源使用同一个接口进行访问. Spring 提供

spring mvc 资源映射配置

在springmvc配置文件中添加 <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/js/" mapping="/js/**"/> - mapping代表映射路径 - location代表文件路径(必须是webapp根目录下的路径才行) - cache-period代表浏览器缓存时间 当我们用前端

Spring配置DataSource数据源

在Spring框架中有如下3种获得DataSource对象的方法: 1.从JNDI获得DataSource. 2.从第三方的连接池获得DataSource. 3.使用DriverManagerDataSource获得DataSource. 一.从JNDI获得DataSource SpringJNDI数据源配置信息: <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean&qu

Spring配置数据源的常用方式

Spring配置数据源的常用方式 在应用程序中配置数据源 (1).在classpath中创建连接参数的配置文件,如db-config.properties,内容如下: driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/zzp username=root password=admin (2).在Spring的配置文件中引入参数配置文件,代码如下: <!-- 配置spring资源文件 --> <bean id=

【转载】Spring加载resource时classpath*:与classpath:的区别

免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:kyfxbl     原文地址: spring配置中classpath和classpath*的区别   在spring配置文件里,可以用classpath:前缀,来从classpath中加载资源  比如在src下有一个jdbc.properties的文件,可以用如下方法加载: <bean id="propertyConfigurer" class="

Spring的资源详解

一.Spring的资源详解 1.1引言 在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理URL资源.File资源.ClassPath相关资源.服务器相关资源等等很多资源.因此处理这些资源需要使用不同的接口,这就增加了我们系统的复杂性:而且处理这些资源步骤都是类似的(打开资源.读取资源.关闭资源),因此如果能抽象出一个统一的接口来对这些底层资源进行统一访问,是不是很方便,而且使我们系统更加简洁,都是对不同的底层资源使用同一个接口进行访问. Spring提供一个Resource接口来统

spring配置与使用

一,关于spring概念的理解 1,spring可以看做是项目的管家,负责管理项目中的所有对象(包括web层,service层和dao层). 2,spring一站式框架:spring跨年性质是属于容器性质的,它之所以能够处理请求,是因为容器中装了处理请求的对象,容器中装了什么对象它就具备什么功能,所以称之为一站式. 3,spring的核心是IOC反转控制和AOP面向切面 4,spring不排斥任何开源框架,并且能够帮助其他框架管理对象 spring中IOC和DI的概念 ioc 反转控制 就是将我

spring配置hibernate的sessionFactory的几种方法

分类: JAVA Spring Hibernate 2013-01-27 20:47  1851人阅读  评论(0)  收藏  举报 spring配置hibernate的sessionFactory 之前用spring2+hibernate3+struts2开发了一个彩信发布系统,由于第一次使用此架构,造成applicationContext.xml中的配置非常冗长,而且经常因为更改一个小配置项(例:数据库ip.用户名.密码等)将此文件作修改,这及不利于项目维护,万一粗心造成其他地方变动,会对本