1、内置的Resource实现
There are a number of Resource implementations that come supplied straight out of the box in Spring:
1) UrlResource
The UrlResource wraps a java.net.URL, and may be used to access any object that is normally accessible via a URL, such as files, an HTTP target, an FTP target, etc. All URLs have a standardized String representation, such that appropriate standardized prefixes are used to indicate one URL type from another. This includes file: for accessing filesystem paths, http: for accessing resources via the HTTP protocol, ftp: for accessing resources via FTP, etc.
A UrlResource is created by Java code explicitly using the UrlResource constructor, but will often be created implicitly when you call an API method which takes a String argument which is meant to represent a path. For the latter case, a JavaBeans PropertyEditor will ultimately decide which type of Resource to create. If the path string contains a few well-known (to it, that is) prefixes such as classpath:, it will create an appropriate specialized Resource for that prefix. However, if it doesn't recognize the prefix, it will assume the this is just a standard URL string, and will create a UrlResource.
2) ClassPathResource
This class represents a resource which should be obtained from the classpath. This uses either the thread context class loader, a given class loader, or a given class for loading resources.
This Resource implementation supports resolution as java.io.File if the class path resource resides in the file system, but not for classpath resources which reside in a jar and have not been expanded (by the servlet engine, or whatever the environment is) to the filesystem. To address this the various Resource implementations always support resolution as a java.net.URL.
A ClassPathResource is created by Java code explicitly using the ClassPathResource constructor, but will often be created implicitly when you call an API method which takes a String argument which is meant to represent a path. For the latter case, a JavaBeans PropertyEditor will recognize the special prefix classpath:on the string path, and create a ClassPathResource in that case.
3) FileSystemResource
This is a Resource implementation for java.io.File handles. It obviously supports resolution as a File, and as a URL.
4) ServletContextResource
This is a Resource implementation for ServletContext resources, interpreting relative paths within the relevant web application's root directory.
This always supports stream access and URL access, but only allows java.io.File access when the web application archive is expanded and the resource is physically on the filesystem. Whether or not it's expanded and on the filesystem like this, or accessed directly from the JAR or somewhere else like a DB (it's conceivable) is actually dependent on the Servlet container.
5) InputStreamResource
A Resource implementation for a given InputStream. This should only be used if no specific Resource implementation is applicable. In particular, prefer ByteArrayResource or any of the file-based Resource implementations where possible.
In contrast to other Resource implementations, this is a descriptor for an already opened resource - therefore returning true from isOpen(). Do not use it if you need to keep the resource descriptor somewhere, or if you need to read a stream multiple times.
6) ByteArrayResource
This is a Resource implementation for a given byte array. It creates a ByteArrayInputStream for the given byte array.
It's useful for loading content from any given byte array, without having to resort to a single-use InputStreamResource.
2、The ResourceLoader
The ResourceLoader interface is meant to be implemented by objects that can return (i.e. load) Resource instances.
public interface ResourceLoader {
Resource getResource(String location);
}
All application contexts implement the ResourceLoader interface, and therefore all application contexts may be used to obtain Resource instances.
When you call getResource() on a specific application context, and the location path specified doesn't have a specific prefix, you will get back a Resource type that is appropriate to that particular application context. For example, assume the following snippet of code was executed against a ClassPathXmlApplicationContext instance:
Resource template = ctx.getResource("some/resource/path/myTemplate.txt");
What would be returned would be a ClassPathResource; if the same method was executed against a FileSystemXmlApplicationContext instance, you'd get back a FileSystemResource. For a WebApplicationContext, you'd get back a ServletContextResource, and so on.
As such, you can load resources in a fashion appropriate to the particular application context.
On the other hand, you may also force ClassPathResource to be used, regardless of the application context type, by specifying the special classpath: prefix:
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");
Similarly, one can force a UrlResource to be used by specifying any of the standard java.net.URL prefixes:
Resource template = ctx.getResource("file:/some/resource/path/myTemplate.txt");
Resource template = ctx.getResource("http://myhost.com/resource/path/myTemplate.txt");
The following table summarizes the strategy for converting Strings to Resources:
- 大小: 66.1 KB
分享到:
相关推荐
数据源的配置是通过在Tomcat的`Context.xml`中添加`<Resource>`元素完成的。这个资源定义了一个名为`jdbc/wb_mysql`的数据源,使用MySQL的JDBC驱动(`com.mysql.jdbc.Driver`)连接到本地的MySQL数据库(地址:`jdbc:...
### SPRING面试宝典知识点详解 #### 一、Spring概述 ...以上就是从给定文件的标题、描述、标签和部分内容中提取出的相关知识点详解,希望能帮助读者更好地理解和掌握Spring框架的核心概念和技术要点。
- **@Resource:** 默认按名称注入,若找不到则按类型注入,更灵活且常用。 #### 五、Spring AOP配置与应用 **概念:** AOP(Aspect Oriented Programming)是面向切面编程,用于处理横切关注点如事务管理、日志记录...
### Spring入门到应用 #### Spring框架概述 - **Spring**是一个开源框架,旨在解决企业级应用开发中的复杂问题。它的核心特性包括松耦合、AOP(面向...掌握Spring的基本概念和技术要点,对于Java开发者来说至关重要。
### Spring3.0 + Struts2.1 + Hibernate3.5 融合步骤 ...以上步骤涵盖了整合的基本要点,但在实际项目中可能还需要根据具体需求进行更详细的调整。希望本文能够帮助读者更好地理解和实践 SSH 架构。
毕业设计-分布式软件测试管理系统的设计与实现。 1项目模块 common 通用依赖、接口等 provide 服务提供方,...采用vuejs + webpack + vue各大组件(vue-router、vue-resource等) ui采用bootstrap、elementui等 其他插件
在Spring MVC框架中,访问图片是一项常见的需求,它...理解这些要点,可以确保你的Spring MVC应用能够流畅地处理图片的显示和访问。在实际项目中,还需要考虑性能优化,比如使用缓存、CDN加速等策略,以提高用户体验。
在 Spring Boot 中,application.yml 的格式要点如下:- 键值对使用冒号 : 分隔,如 `key: value`。- 层级结构通过空格缩进表示,每个层级的缩进量必须一致。- 列表使用 `-` 开头,如 `- item1` `- item2`。- 字符串...
这篇文章将深入探讨这个项目的实现细节和技术要点。 首先,Spring PetClinic是一个经典的示例项目,用于演示Spring框架的各种组件和最佳实践。在这个"spring-petclinic-rest-vet"版本中,重点在于构建一个REST API...
以下是Spring MVC的配置要点: 1. **添加Spring MVC依赖**:首先需要在项目中引入Spring MVC的依赖库,通常可以通过Maven或Gradle来管理这些依赖。 2. **配置监听器**:在`web.xml`文件中配置`...
- **Resource**:资源文件夹,存放配置文件、静态资源等。 #### 五、业务流程与功能实现 1. **登录功能**: - HTML页面发送登录请求(url为`/loginIn`)。 - `LoginController`中的`loginIn()`方法接收并处理该...
2. **RESTful API 设计**:REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于 HTTP 协议,通过 URI(Uniform Resource Identifier)定位资源,使用 HTTP 动词(GET, POST, PUT,...
### 从Drools规则引擎到风控反洗钱系统V0.2.3 #### Drools简介 ...希望这些内容能够帮助读者更好地理解和掌握Drools规则引擎的使用方法和技术要点。随着版本的不断演进,Drools的功能和应用场景也将越来越广泛。
接下来,我们将深入探讨这三个框架的主要配置文件及其配置要点。 首先,让我们从Struts2开始。Struts2是一个基于MVC(Model-View-Controller)设计模式的Web框架,它负责处理用户的请求并将其转发到相应的控制器。...
- **Resource接口**:这是Spring框架提供的用于描述资源的标准接口。 - **ResourceLoader和ResourcePatternResolver接口**:这两个接口定义了资源加载器应具备的基本能力。 - **在代码中取得资源**:通过这些接口,...