`
liyixing1
  • 浏览: 967077 次
  • 性别: Icon_minigender_1
  • 来自: 江西上饶
社区版块
存档分类
最新评论

资源 Resource

阅读更多
Resource接口他是继承InputStreamSource

public interface InputStreamSource {

    InputStream getInputStream() throws IOException;

}

Spring的 Resource 接口是为了提供更强的访问底层资源能力的抽象。

/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.core.io;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;

/**
* 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 FileSystemResource
* @see ClassPathResource
* @see UrlResource
* @see ByteArrayResource
* @see InputStreamResource
* @see org.springframework.web.context.support.ServletContextResource
*/
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</code> 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</code> for typical resource descriptors;
* note that actual content reading may still fail when attempted.
* However, a value of <code>false</code> is a definitive indication
* that the resource content cannot be read.
*/
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</code> 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;

/**
* Return a filename for this resource, i.e. typically the last
* part of the path: for example, "myfile.txt".
*/
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</code> method.
* @see java.lang.Object#toString()
*/
String getDescription();

}

Resource 接口一些比较重要的方法如下:

getInputStream(): 定位并打开资源,返回读取此资源的一个 InputStream。每次调用预期会返回一个新的 InputStream,由调用者负责关闭这个流。

exists(): 返回标识这个资源在物理上是否的确存在的 boolean 值。

isOpen(): 返回标识这个资源是否有已打开流的处理类的 boolean 值。如果为 true,则此InputStream 就不能被多次读取, 而且只能被读取一次然后关闭以避免资源泄漏。除了 InputStreamResource,常见的resource实现都会返回 false。

getDescription(): 返回资源的描述,一般在与此资源相关的错误输出时使用。此描述通常是完整的文件名或实际的URL地址。

其它方法让你获得表示该资源的实际的 URL 或 File 对象(如果隐含的实现支持该方法并保持一致的话)。

Spring自身处理资源请求的多种方法声明中将Resource 抽象作为参数而广泛地使用。 Spring APIs中的一些其它方法(比如许多ApplicationContext的实现构造函数),使用普通格式的 String 来创建与context相符的Resource,也可以使用特殊的路径String前缀来让调用者指定创建和使用特定的 Resource 实现。

Resource不仅被Spring自身大量地使用,它也非常适合在你自己的代码中独立作为辅助类使用。 用户代码甚至可以在不用关心Spring其它部分的情况下访问资源。这样的确会造成代码与Spring之间的耦合,但也仅仅是与很少量的辅助类耦合。这些类可以作为比 URL 更有效的替代,而且与为这个目的而使用其它类库基本相似。

需要注意的是 Resource 抽象并没有改变功能:它尽量使用封装。 比如 UrlResource 封装了URL,然后使用被封装的 URL 来工作。

Spring提供了很多 Resource 的实现:

UrlResource

UrlResource 封装了java.net.URL,它能够被用来访问任何通过URL可以获得的对象,例如:文件、HTTP对象、FTP对象等。所有的URL都有个标准的 String表示,这些标准前缀可以标识不同的URL类型,包括file:访问文件系统路径,http: 通过HTTP协议访问的资源,ftp: 通过FTP访问的资源等等。

UrlResource 对象可以在Java代码中显式地使用 UrlResource 构造函数来创建。但更多的是通过调用带表示路径的 String 参数的API函数隐式地创建。在后一种情况下,JavaBeans的 PropertyEditor 会最终决定哪种类型的 Resource 被创建。如果这个字符串包含一些众所周知的前缀,比如 classpath:,它就会创建一个对应的已串行化的 Resource。 然而,如果不能分辨出这个前缀,就会假定它是个标准的URL字符串,然后创建UrlResource。

ClassPathResource

这个类标识从classpath获得的资源。它会使用线程context的类加载器(class loader)、给定的类加载器或者用来载入资源的给定类。

如果类路径上的资源存在于文件系统里,这个 Resource 的实现会提供类似于java.io.File的功能。而如果资源是存在于还未解开(被servlet引擎或其它的环境解开)的jar包中,这些 Resource 实现会提供类似于java.net.URL 的功能。

ClassPathResource对象可以在Java代码中显式地使用ClassPathResource 构造函数来创建。但更多的是通过调用带表示路径的String参数的API函数隐式地创建。在后一种情况下,JavaBeans的 PropertyEditor 会分辨字符串中 classpath: 前缀,然后相应创建 ClassPathResource。

FileSystemResource

这是为处理 java.io.File 而准备的Resource实现。它既可以作为File提供,也可以作为URL。

ServletContextResource

这是为 ServletContext 资源提供的 Resource 实现,它负责解析相关web应用根目录中的相对路径。

它始终支持以流和URL的方式访问。 但是只有当web应用包被解开并且资源在文件系统的物理路径上时,才允许以 java.io.File 方式访问。是否解开并且在文件系统中访问,还是直接从JAR包访问或以其它方式访问如DB(这是可以想象的),仅取决于Servlet容器。

InputStreamResource

这是为给定的 InputStream 而准备的 Resource 实现。它只有在没有其它合适的 Resource 实现时才使用。而且,只要有可能就尽量使用 ByteArrayResource 或者其它基于文件的Resource 实现。

与其它 Resource 实现不同的是,这是个 已经 打开资源的描述符-因此 isOpen() 函数返回 true。 如果你需要在其它位置保持这个资源的描述符或者多次读取一个流,请不要使用它。

ByteArrayResource

这是为给定的byte数组准备的 Resource 实现。 它会为给定的byte数组构造一个 ByteArrayInputStream。

它在从任何给定的byte数组读取内容时很有用,因为不用转换成单一作用的 InputStreamResource。

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");
同样的,你可以用任何标准的 java.net.URL 前缀,强制使用 UrlResource :

Resource template = ctx.getResource("file:/some/resource/path/myTemplate.txt");
Resource template = ctx.getResource("http://myhost.com/resource/path/myTemplate.txt");
下面的表格概述了 String 到 Resource 的转换规则:

Table 4.1. Resource strings

前缀 例子 说明
classpath:

classpath:com/myapp/config.xml

从classpath中加载。

file:

file:/data/config.xml

作为 URL 从文件系统中加载。[a]

http:

http://myserver/logo.png

作为 URL 加载。

(none)

/data/config.xml

根据 ApplicationContext 进行判断。
分享到:
评论

相关推荐

    Resource.zip 资源Resource的定义及使用案例

    "Resource.zip" 文件很可能包含了一系列的资源文件,用于一个WPF应用项目。 在WPF中,资源分为两类:静态资源和动态资源。静态资源在编译时被解析,而动态资源则在运行时解析。这两种类型的资源都有各自的用途和...

    Oracle 12c资源管理器resource manager

    ### Oracle 12c资源管理器(resource manager)详解 #### 一、Oracle 数据库资源管理器概述 在Oracle 12c中,资源管理器(Resource Manager)是一个强大的工具,它能够帮助管理员有效地管理数据库中的多个工作负载,...

    资源提取软件ResourceExtractor

    ResourceExtractor,可以方便的提取exe、dll中的资源。

    Resource Hacker 修改软件资源的好工具

    《Resource Hacker:深入解析软件资源修改的艺术》 Resource Hacker,这款强大的工具,是软件开发者、本地化人员以及对软件有个性化需求的用户们的得力助手。它专为修改软件资源而设计,无论是汉化程序,还是替换...

    Resource Binder v3.1 -EXE资源优化重建工具

    Resource Binder v3.1是一款强大的EXE资源优化与重建工具,专为IT专业人士设计,用于处理PE(Portable Executable)文件格式的资源管理。这款工具的主要功能是帮助用户对程序的资源部分进行编辑、更新和优化,从而...

    EXE DLL资源抽取工具箱(Resource hacker, Resource Extract, ExeScope)

    首先,Resource Hacker是一款强大的资源编辑器,它能够打开、查看、修改和保存EXE、DLL、OCX等文件中的资源。你可以通过Resource Hacker来查看并修改程序的图标、对话框、菜单、字符串表等元素。它还支持添加、删除...

    Resource Hacker资源修改器

    Resource Hacker是非常强大的资源查看器,编译和反编译工具!Resource Hacker 主要用于察看、反汇编和取代 Windows 32 位可执行程序中的资源,支持 Delphi 5 和 C++ Builder。

    wpf-资源的使用-resource

    1. **资源字典(Resource Dictionary)** - 资源通常存储在资源字典中,这是一个键值对的集合,其中键是唯一的标识符,值是资源本身。 - 资源字典可以通过`.xaml`文件创建,并可以嵌入到窗口、控件或者应用程序...

    C#Resource文件(资源档案)修改工具

    在.NET框架中,资源文件(Resource Files)是用于存储应用程序中使用的各种静态数据,如文本字符串、图像、图标等的容器。C#项目通常会利用这些资源文件来管理多语言支持、图标和常量字符串,使得开发过程更加灵活且...

    VC RES资源修复工具,以及将resource.h资源重新排序,整合。-VC resource repair tool.

    这个工具主要用于修复VC++项目中的资源问题,并能对`resource.h`文件中的资源ID进行排序和整合。 `resource.h`文件是存储资源定义的地方,它包含了每个资源的唯一标识符(ID)。当项目发展过程中,资源ID可能因为...

    EXE/DLL文件中的资源(位图、图标、字符串等)查看器

    EXE/DLL文件中的资源(位图、图标、字符串等)查看器。以及版本信息等。代码较老,是VC6的项目工程,不过还是可用的,值得参考

    资源分配器resource manager

    资源分配器,通常被称为Resource Manager,是IT系统中一个至关重要的组件,特别是在分布式计算环境或者云服务中。它主要负责管理和调度系统中的各种资源,包括CPU时间、内存、磁盘空间以及网络带宽等,确保这些资源...

    qnx系统下Resource Maneger 最简单的例子

    本文将对 QNX 系统下 Resource Manager 的最简单例子进行详细解释,介绍如何使用 Resource Manager API 实现一个简单的资源管理器。 Resource Manager 是 QNX 操作系统中的一种机制,用于管理系统资源,例如进程、...

    ResHacker.exe强大的资源替换工具 resource_hacker.zip

    Resource Hacker是一款免费查看,修改,添加,删除和重命名,提取Windows可执行文件和资源文件的资源替换工具,Resource Hacker反编译工具是相当于eXeScope的反编译工具,并且有很多方面比eXeScope反编译还强的软件.

    Resource 5.1.7 繁体可用.zip

    《EXE资源提取工具——Resource Hacker详解》 在软件开发过程中,EXE程序中的资源文件起着至关重要的作用,它们包含应用程序的图标、菜单、对话框等元素。有时,我们可能需要修改或提取这些资源,这时就需要用到...

    资源替换工具 Resource Hacker

    用于查看,修改,添加和删除 Win32 可执行文件的资源。内置了一个内部资源编译器和反编译器。

    OneNET NBIoT订阅资源Object Resource 手册

    根据提供的信息,OneNET NBIoT订阅资源Object Resource手册是关于如何使用OneNET平台对NBIoT(窄带物联网)设备进行订阅资源管理的技术文档。OneNET是中国移动推出的物联网开放平台,其支持多种物联网技术标准,包括...

    resources_resource编号排序_

    在这个特定的上下文中,我们关注的是“resources_resource编号排序_”标题所指的“资源文件RESOURCE.H”,它是MFC应用中的一个重要组件。 资源文件(通常命名为RC文件)是包含应用程序界面元素定义的地方,如菜单、...

    exe资源修改编辑器Resource Hacker3.5.2

    《exe资源修改编辑器Resource Hacker 3.5.2详解》 在IT行业中,软件开发是一项复杂而精细的工作,涉及到诸多细节。对于某些特定需求,开发者可能需要对已编译的EXE文件进行修改,例如调整欢迎界面、替换文本等。...

    Resource Builder3是一款可视化Delphi资源编辑器,功能强大,含Key

    Resource Builder3是一款可视化Delphi资源编辑器,功能强大,含Key

Global site tag (gtag.js) - Google Analytics