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();
分享到:
相关推荐
李白高力士脱靴李白贺知章告别课本剧.pptx
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
C语言项目之超级万年历系统源码,可以做课程设计参考 文章参考:https://www.qqmu.com/4373.html
Jupyter-Notebook
51单片机加减乘除计算器系统设计(proteus8.17,keil5),复制粘贴就可以运行
《中国房地产统计年鉴》面板数据资源-精心整理.zip
Jupyter-Notebook
Jupyter-Notebook
毕业论文答辩ppt,答辩ppt模板,共18套
Jupyter-Notebook
《中国城市统计年鉴》面板数据集(2004-2020年,最新).zip
Python基础 本节课知识点: • set的定义 • Set的解析 • set的操作 • set的函数
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
兵制与官制研究资料最新版.zip
Jupyter-Notebook
七普人口数据+微观数据+可视化+GIS矢量资源-精心整理.zip
Support package for Hovl Studio assets.unitypackage
土壤数据库最新集.zip
Jupyter-Notebook
1991-2020年中国能源统计年鉴-能源消费量(分省)面板数据-已更至最新.zip