1:The Resource interface
引用
Spring's Resource interface is meant to be a more capable interface for abstracting access to low-level
resources.
public interface Resource extends InputStreamSource {
boolean exists();
boolean isOpen();
URL getURL() throws IOException;
File getFile() throws IOException;
Resource createRelative(String relativePath) throws IOException;
String getFilename();
String getDescription();
}
public interface InputStreamSource {
InputStream getInputStream() throws IOException;
}
2:Built-in Resource implementations
UrlResource
ClassPathResource
引用
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.
FileSystemResource
ServletContextResource
InputStreamResource
ByteArrayResource
3: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);
}
引用
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
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
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");
4:Resources as dependencies
引用
What makes it trivial to then inject these properties, is that all application contexts register and use a
special JavaBeans PropertyEditor which can convert String paths to Resource objects. So if
myBean has a template property of type Resource, it can be configured with a simple string for that
resource, as follows:
<bean id="myBean" class="...">
<property name="template" value="some/resource/path/myTemplate.txt"/>
</bean>
If there is a need to force a specific Resource type to be used, then a prefix may be used. The following
two examples show how to force a ClassPathResource and a UrlResource (the latter being used
to access a filesystem file).
<property name="template" value="classpath:some/resource/path/myTemplate.txt">
<property name="template" value="file:/some/resource/path/myTemplate.txt"/>
5:Application contexts and Resource paths
When such a location path doesn't have a prefix, the specific Resource type built from that path andused to load the bean definitions, depends on and is appropriate to the specific application context. For example, if you create a ClassPathXmlApplicationContext as follows:
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");
The bean definitions will be loaded from the classpath, as a ClassPathResource will be used
if you create a FileSystemXmlApplicationContext as follows:
ApplicationContext ctx =
new FileSystemXmlApplicationContext("conf/appContext.xml");
The bean definition will be loaded from a filesystem location, in this case relative to the current working
directory
The basic idea is that one supplies merely a string array containing just the filenames of the
XML files themselves (without the leading path information), and one also supplies a Class; the
ClassPathXmlApplicationContext will derive the path information from the supplied class.
com/
foo/
services.xml
daos.xml
MessengerService.class
ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] {"services.xml", "daos.xml"}, MessengerService.class);
some Examples
String filePath="D:/TJ_project/spring/conf/test.properties";
Resource res1=new FileSystemResource(filePath);
Resource res2=new ClassPathResource("test.properties");
InputStream ins1 = res1.getInputStream();
InputStream ins2 = res2.getInputStream();
System.out.println(res1.getFilename());
System.out.println(res2.getFilename());
Resource resource=new ClassPathResource("hello.xml");
BeanFactory factory=new XmlBeanFactory(resource);
T tt=(T)factory.getBean("tt");
tt.method();
分享到:
相关推荐
标题 "resources.pak 打包工具 V4 V5" 指的是用于处理 Chrome 浏览器中的 resources.pak 文件的工具,这是一个包含了浏览器界面元素、字符串和其他资源的压缩文件。资源 pak 文件是 Google Chrome 为了优化加载速度...
"resources.assets.zip"这个文件名暗示了它可能是一个与资源管理和打包相关的压缩文件,通常用于存储应用程序或游戏中的各种静态资产,如图像、音频、纹理、3D模型、文本等。在Unity等游戏引擎中,这种格式非常常见...
资源文件如`.resources`是.NET Framework用于存储应用程序中的非代码数据,如字符串、图标、图像等。`.resx`文件则是另一种形式的资源文件,它以XML格式存储,允许更直观地编辑和管理资源。本篇文章将详细阐述如何将...
(1) Manifest Resources(资源清单) 资源在编译期间添加到程序集。如果要将资源嵌入到程序集,则必须将文件添加到项目中,文件会自动拷贝到项目文件夹的Resources文件夹中。如果要嵌入到程序集,还需选中文件,修改...
《全面解析Save All Resources 2.0.4插件及其CRX格式》 在互联网的广阔天地中,浏览器扩展程序扮演着至关重要的角色,它们为用户提供了一种方便的方式来增强和定制浏览器的功能。其中,“Save All Resources 2.0.4...
1、将.\Webstorm 2017.1\lib目录下的resources_en.jar文件复制出来,并更名为resources_cn.jar。 2、双击打开resources_cn.jar(注意是打开而不是解压出来),将下载的汉化包zh_CN目录下的所有文件拖到刚才打开的...
本文将详细介绍如何利用*.resources.dll文件进行Devexpress的汉化,并探讨相关工具和步骤。 首先,了解*.resources.dll文件。这是一个.NET Framework中的资源文件,它包含了应用程序中使用的字符串、图像和其他本地...
在某些情况下,开发者可能需要将反编译得到的.resources文件转换为更易于管理和编辑的.resx文件格式。 标题提到的"Reflector批量转换resources文件为resx工具"是一个专门解决这个问题的实用工具。.resources文件是...
标题中的“Save All Resources”是一款针对谷歌浏览器(Chrome)的扩展程序,它的主要功能是帮助用户一键下载网页中所有的资源文件。这款插件对于开发者、设计师或者需要批量下载网页资源的用户来说非常实用,它能...
resources_cn_IntelliJIDEA_2017.3.1_r2.jar resources_cn_IntelliJIDEA_2017.3.2_r1.jar resources_cn_IntelliJIDEA_2017.3.3_r2.jar resources_cn_IntelliJIDEA_2017.3.4_r1.jar resources_...
### APK XML解析及反编译之resources.arsc #### 完整Android资源resources.arsc文件分析 在深入探讨`resources.arsc`文件结构及其解析方法之前,我们需要了解`resources.arsc`的基本概念。`resources.arsc`...
resources.a
《NSIS + WinCodeSign + NSIS Resources:创建安全可执行安装程序的综合指南》 在软件开发领域,创建一个可执行的安装程序是至关重要的一步,它使得用户能够便捷地安装和卸载你的应用程序。NSIS (Nullsoft ...
要保存网站内容,一种做法是直接右键另存为整个网页。虽然有时网页也能正常运行,但不可避免地丢失了网站文件夹结构。当然,你也可以在Sources中...3、翻到最后一个选项选择resourcessaver,点击Save All Resources即可
【谷歌浏览器插件_Save All Resources】是一款专为谷歌浏览器(Chrome)设计的实用工具,它允许用户方便地下载开发者工具(F12模式)中显示的所有资源。这款插件对于前端开发者、网页设计师以及网络资源收集者来说...
在Spring Boot应用中,资源文件(resources)是存放应用程序所需的静态内容、模板文件以及配置文件的地方。当项目被打成jar包后,有时可能会遇到下载这些资源文件时返回空或者找不到的情况。这个问题通常与Spring ...
"resources.pak"打包工具是用于处理Chrome浏览器中资源文件的一种特定格式的打包程序。在Chrome浏览器中,为了提高性能和安全性,许多静态资源如HTML、CSS、JavaScript、图片等会被压缩并打包成"resources.pak"文件...
【标题】"Chrome插件0.1.8:Save All Resources "是一款专为Chrome浏览器设计的工具,旨在帮助用户批量保存网页中的所有资源。这款插件的版本号为0.1.8,意味着它可能已经经过多次迭代和优化,以提供更好的资源下载...
标题中的"resources_cn.rar"是一个压缩文件,通常用于存储多个相关文件或文件夹,以便于传输和管理。这种格式在IT行业中广泛使用,因为它可以减少文件的大小,节省存储空间,并便于通过网络进行快速分享。RAR是一种...
在Android开发中,String Resources是用于管理应用内所有文本内容的重要工具。它允许开发者在一个集中的地方定义字符串,便于维护、本地化以及动态格式化。本文将深入探讨Android String Resources的使用,包括国际...