【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
来源:互联网 发布:linux内核开发流程 编辑:程序博客网 时间:2024/04/30 17:59
文章转自:http://sishuok.com/forum/blogPost/list/2457.html
4.3.1 ResourceLoader接口
ResourceLoader接口用于返回Resource对象;其实现可以看作是一个生产Resource的工厂类。
getResource接口用于根据提供的location参数返回相应的Resource对象;而getClassLoader则返回加载这些Resource的ClassLoader。
Spring提供了一个适用于所有环境的DefaultResourceLoader实现,可以返回ClassPathResource、UrlResource;还提供一个用于web环境的ServletContextResourceLoader,它继承了DefaultResourceLoader的所有功能,又额外提供了获取ServletContextResource的支持。
ResourceLoader在进行加载资源时需要使用前缀来指定需要加载:“classpath:path”表示返回ClasspathResource,“http://path”和“file:path”表示返回UrlResource资源,如果不加前缀则需要根据当前上下文来决定,DefaultResourceLoader默认实现可以加载classpath资源,如代码所示(cn.javass.spring.chapter4.ResourceLoaderTest):
对于目前所有ApplicationContext都实现了ResourceLoader,因此可以使用其来加载资源。
ClassPathXmlApplicationContext:不指定前缀将返回默认的ClassPathResource资源,否则将根据前缀来加载资源;
FileSystemXmlApplicationContext:不指定前缀将返回FileSystemResource,否则将根据前缀来加载资源;
WebApplicationContext:不指定前缀将返回ServletContextResource,否则将根据前缀来加载资源;
其他:不指定前缀根据当前上下文返回Resource实现,否则将根据前缀来加载资源。
4.3.2 ResourceLoaderAware接口
ResourceLoaderAware是一个标记接口,用于通过ApplicationContext上下文注入ResourceLoader。
让我们看下测试代码吧:
1) 首先准备测试Bean,我们的测试Bean还简单只需实现ResourceLoaderAware接口,然后通过回调将ResourceLoader保存下来就可以了:
2) 配置Bean定义(chapter4/resourceLoaderAware.xml):
3)测试(cn.javass.spring.chapter4.ResoureLoaderAwareTest):
注意此处“loader instanceof ApplicationContext”,说明了ApplicationContext就是个ResoureLoader。
由于上述实现回调接口注入ResourceLoader的方式属于侵入式,所以不推荐上述方法,可以采用更好的自动注入方式,如“byType”和“constructor”,此处就不演示了。
4.3.3 注入Resource
通过回调或注入方式注入“ResourceLoader”,然后再通过“ResourceLoader”再来加载需要的资源对于只需要加载某个固定的资源是不是很麻烦,有没有更好的方法类似于前边实例中注入“java.io.File”类似方式呢?
Spring提供了一个PropertyEditor “ResourceEditor”用于在注入的字符串和Resource之间进行转换。因此可以使用注入方式注入Resource。
ResourceEditor完全使用ApplicationContext根据注入的路径字符串获取相应的Resource,说白了还是自己做还是容器帮你做的问题。
接下让我们看下示例:
1)准备Bean:
2)准备配置文件(chapter4/ resourceInject.xml):
注意此处“resourceBean1”注入的路径没有前缀表示根据使用的ApplicationContext实现进行选择Resource实现。
3)让我们来看下测试代码(cn.javass.spring.chapter4.ResourceInjectTest)吧:
接下来一节让我们深入ApplicationContext对各种Resource的支持,及如何使用更便利的资源加载方式。
- 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
- 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
- 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
- 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
- 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
- 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
- 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
- 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
- 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
- 【第四章】 资源 之 4.2 内置Resource实现 ——跟我学spring3
- 【第四章】 资源 之 4.4 Resource通配符路径 ——跟我学spring3
- 【第四章】 资源 之 4.2 内置Resource实现 ——跟我学spring3
- 【第四章】 资源 之 4.4 Resource通配符路径 ——跟我学spring3
- 【第四章】 资源 之 4.2 内置Resource实现 ——跟我学spring3
- 【第四章】 资源 之 4.4 Resource通配符路径 ——跟我学spring3
- 【第四章】 资源 之 4.2 内置Resource实现 ——跟我学spring3
- 【第四章】 资源 之 4.4 Resource通配符路径 ——跟我学spring3
- 【第四章】 资源 之 4.2 内置Resource实现 ——跟我学spring3
- 不加<!DOCTYPE html>页面也显示,加不加的区别
- 最长上升子列
- 无法确定表达式的类型,因为<null>和int之间没有隐式转换
- 学习jquery小测试 自娱自乐
- null和""的区别详解
- 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
- 信号量的初始值
- linux驱动中的等待队列头
- UVa - 156 Ananagrams(STL)
- 关于Ping和Tracert命令原理详解
- 1-2 Fibonacci sequence
- UVa - 10474 Where is the Marble?(STL)
- Android开发本地及网络Mp3音乐播放器(十五)网络音乐及歌词下载功能实现
- UVa - 10815 Andy's First Dictionary(STL)