【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
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):
- @Test
- public void testResourceLoad() {
- ResourceLoader loader = new DefaultResourceLoader();
- Resource resource = loader.getResource("classpath:cn/javass/spring/chapter4/test1.txt");
- //验证返回的是ClassPathResource
- Assert.assertEquals(ClassPathResource.class, resource.getClass());
- Resource resource2 = loader.getResource("file:cn/javass/spring/chapter4/test1.txt");
- //验证返回的是ClassPathResource
- Assert.assertEquals(UrlResource.class, resource2.getClass());
- Resource resource3 = loader.getResource("cn/javass/spring/chapter4/test1.txt");
- //验证返默认可以加载ClasspathResource
- Assert.assertTrue(resource3 instanceof ClassPathResource);
- }
对于目前所有ApplicationContext都实现了ResourceLoader,因此可以使用其来加载资源。
ClassPathXmlApplicationContext:不指定前缀将返回默认的ClassPathResource资源,否则将根据前缀来加载资源;
FileSystemXmlApplicationContext:不指定前缀将返回FileSystemResource,否则将根据前缀来加载资源;
WebApplicationContext:不指定前缀将返回ServletContextResource,否则将根据前缀来加载资源;
其他:不指定前缀根据当前上下文返回Resource实现,否则将根据前缀来加载资源。
4.3.2 ResourceLoaderAware接口
ResourceLoaderAware是一个标记接口,用于通过ApplicationContext上下文注入ResourceLoader。
让我们看下测试代码吧:
1) 首先准备测试Bean,我们的测试Bean还简单只需实现ResourceLoaderAware接口,然后通过回调将ResourceLoader保存下来就可以了:
- package cn.javass.spring.chapter4.bean;
- import org.springframework.context.ResourceLoaderAware;
- import org.springframework.core.io.ResourceLoader;
- public class ResourceBean implements ResourceLoaderAware {
- private ResourceLoader resourceLoader;
- @Override
- public void setResourceLoader(ResourceLoader resourceLoader) {
- this.resourceLoader = resourceLoader;
- }
- public ResourceLoader getResourceLoader() {
- return resourceLoader;
- }
- }
2) 配置Bean定义(chapter4/resourceLoaderAware.xml):
3)测试(cn.javass.spring.chapter4.ResoureLoaderAwareTest):
- @Test
- public void test() {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter4/resourceLoaderAware.xml");
- ResourceBean resourceBean = ctx.getBean(ResourceBean.class);
- ResourceLoader loader = resourceBean.getResourceLoader();
- Assert.assertTrue(loader instanceof ApplicationContext);
- }
注意此处“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):
- <bean id="resourceBean1" class="cn.javass.spring.chapter4.bean.ResourceBean3">
- <property name="resource" value="cn/javass/spring/chapter4/test1.properties"/>
- </bean>
- <bean id="resourceBean2" class="cn.javass.spring.chapter4.bean.ResourceBean3">
- <property name="resource"
- value="classpath:cn/javass/spring/chapter4/test1.properties"/>
- </bean>
注意此处“resourceBean1”注入的路径没有前缀表示根据使用的ApplicationContext实现进行选择Resource实现。
3)让我们来看下测试代码(cn.javass.spring.chapter4.ResourceInjectTest)吧:
- @Test
- public void test() {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter4/resourceInject.xml");
- ResourceBean3 resourceBean1 = ctx.getBean("resourceBean1", ResourceBean3.class);
- ResourceBean3 resourceBean2 = ctx.getBean("resourceBean2", ResourceBean3.class);
- Assert.assertTrue(resourceBean1.getResource() instanceof ClassPathResource);
- Assert.assertTrue(resourceBean2.getResource() instanceof ClassPathResource);
- }
接下来一节让我们深入ApplicationContext对各种Resource的支持,及如何使用更便利的资源加载方式。
原创内容 转自请注明出处【http://sishuok.com/forum/blogPost/list/0/2457.html】
相关推荐
适合初学者spring-boot——web笔记
【第四章】 资源 之 4.2 内置Resource实现 ——跟我学spring3 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3 【第四章】 资源 之 4.4 Resource通配符路径 ——跟我学spring3 【第五章】Spring表达式语言 ...
【第八章】 对ORM的支持 之 8.1 概述 ——跟我学spring3 【第八章】 对ORM的支持 之 8.2 集成Hibernate3 ——跟我学spring3 【第八章】 对ORM的支持 之 8.3 集成iBATIS ——跟我学spring3 【第八章】 对ORM的支持 之...
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
spring mybatis struts2的一个框架整合,列出主要代码
spring.jar spring-aop.jar spring-aop.jar spring-beans.jar spring-hibernate3.jar spring-jdbc.jar spring-struts.jar spring-web.jar
最新版spring-web-5.2.3.RELEASE和spring-webmvc-5.2.3.RELEASE
spring-context-1.2.8.jar, spring-context-1.2.9.jar, spring-context-2.0-m2.jar, spring-context-2.0.1.jar, spring-context-2.0.2.jar, spring-context-2.0.4.jar, spring-context-2.0.6.jar, spring-context-...
spring-resource-handling, Spring Framework 4.1资源处理示例 spring-资源处理 这里应用程序演示 Spring Framework 4.1中的新资源处理功能。 它最初是为在 4.1中讨论资源而开发的,在 SpringOne2GX 2014中讨论。...
spring源码缺失jar。spring-cglib-repack-3.2.5.jar,spring-objenesis-repack-2.6.jar
spring源码导入SpringObjenesis类报错解决,缺少jar包spring-cglib-repack-3.2.0.jar和spring-objenesis-repack-2.1.jar
spring-cglib-repack-3.2.5.jar和spring-objenesis-repack-2.6.jar
使用spring-cloud-security-oauth2来实现oauth server和resource server,oauth Server和resource Server分开,resource Server实现了两种方式
spring-boot项目pom.xm中parent依赖文件spring-boot-starter-parent-1.5.13.RELEASE.pom
spring-aop-4.1.6.RELEASE.jar spring-aspects-4.1.6.RELEASE.jar spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-context-support-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.jar ...
spring-boot spring-security-oauth2 完整demo,可以使用微信的方式来获取token和查看资源,注意看代码中的备注
spring-session+spring依赖jar包,包含spring4.0.2.RELEASE相关jar包和commons-pool2-2.4.2.jar,jedis-2.7.3.jar,spring-data-redis-1.6.2.RELEASE.jar,spring-session-1.1.1.RELEASE.jar
spring-framework-3.0.5.RELEASE-dependencies spring依赖的jar包
spring-web-5.0.4.RELEASE.jar; spring-web-5.0.4.RELEASE.jar
spring-web.jar spring-web.jar