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();
分享到:
相关推荐
(1) Manifest Resources(资源清单) 资源在编译期间添加到程序集。如果要将资源嵌入到程序集,则必须将文件添加到项目中,文件会自动拷贝到项目文件夹的Resources文件夹中。如果要嵌入到程序集,还需选中文件,修改...
1、将.\Webstorm 2017.1\lib目录下的resources_en.jar文件复制出来,并更名为resources_cn.jar。 2、双击打开resources_cn.jar(注意是打开而不是解压出来),将下载的汉化包zh_CN目录下的所有文件拖到刚才打开的...
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_...
resources.a
maven-resources-plugin-3.1.0.jar
mscorlib.dll-resources.dat
运行环境.netframwork 4.0; 窗体右上角点击off按钮,将开启智能提醒帮助; 一键转换整个目录内大批量的resources文件; 如果某个文件有问题,将会逐个提醒问题文件,是一个难得的工具; 欢迎提出问题!
maven-resources-plugin-2.4.1.jar
C#反编译资源文件修复工具ILSpy Reflector resgen批量resources转换resx工具 ILSpy反编译后,Winform窗体中的resx文件全部变成了resources,为了方便查看代码,得把这些转回来,这个工具批量转换非常方便
☆ 资源说明:☆ DevExpress Localized Resources 2014.1 DXperience 14.1 DC 2014-06-09 (dxKB A421) DXperience ASP 14.1 DC 2014-6-6 (dxKB K421)
Reflector反编译后得到的resources文件批量转换为resx ,内含了resgen.exe,自动去除文件名的顶层名字空间,可以直接导入到反编译后的项目中去,自己弄的时候感觉很麻烦,写了这个小工具,给大家节省点时间,5分应该...
完整Android资源resources.arsc文件分析
modify from http://code.google.com/p/android-unused-resources/ ... usage : java -jar android_unused_resources_advance.jar androidProjectPath isDelete, please backup your project before run, have fun
This book is a collection of in-depth guides to some some of the tools and resources most used with React, such as Jest and React Router, as well as a discussion about how React works well with D3, ...
解压后,将文件“resources_cn.jar”复制到 JetBrains\PyCharm Community Edition xx.xx\lib目录下面,重启即可显示中文 该中文字库支持在以下系统使用:Windows、MacOS、Linux 解压后,将解压后的两个‘.jar’文件...
android studio创建9.patch图片,使用时出现Error: Duplicate resources 笔者运行环境:MacOs Catalina , android studio 3.6.3 原创文章 5获赞 2访问量 249 关注 私信 展开阅读全文 作者:徐州捕快
L05 Resources and Trade The Heckscher-Ohlin Model
调用resgen.exe 批量转换resources为resx
Spring Boot项目打成jar包后,下载resources目录下文件为空的demo
resources.pak 打包工具,。