4.3.1  ResourceLoader接口

ResourceLoader接口用于返回Resource对象;其实现可以看作是一个生产Resource的工厂类。

public interface ResourceLoader {
Resource getResource(String location);
ClassLoader getClassLoader();
}

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。

public interface ResourceLoaderAware {
void setResourceLoader(ResourceLoader 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):

<bean class="cn.javass.spring.chapter4.bean.ResourceBean"/>  

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:

package cn.javass.spring.chapter4.bean;
import org.springframework.core.io.Resource;
public class ResourceBean3 {
private Resource resource;
public Resource getResource() {
return resource;
}
public void setResource(Resource resource) {
this.resource = resource;
}
}

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的支持,及如何使用更便利的资源加载方式。

资源 之 4.3 访问Resource(拾壹)的更多相关文章

  1. 开涛spring3(4.3) - 资源 之 4.3 访问Resource

    4.3.1  ResourceLoader接口 ResourceLoader接口用于返回Resource对象:其实现可以看作是一个生产Resource的工厂类. public interface Re ...

  2. spring-第十四篇之资源访问Resource接口

    1.Resource接口提供的主要方法 1>getInputStream():定位并打开资源,返回资源对应的输入流.每次调用都返回新的输入流.调用者必须负责关闭输入流. 2>isOpen( ...

  3. Hadoop 2.0 中的资源管理框架 - YARN(Yet Another Resource Negotiator)

    1. Hadoop 2.0 中的资源管理 http://dongxicheng.org/mapreduce-nextgen/hadoop-1-and-2-resource-manage/ Hadoop ...

  4. springmvc:BeanNameViewResolver访问内部资源视图对象和访问外部资源视图对象

    <!-- 处理器映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerM ...

  5. ASP.NET 通过配置hiddenSegment禁止目录下资源通过Url形式访问

    根据默认的ASP.NET配置,App_Data下的资源是禁止通过Url形式直接访问的,在实际开发中,可能也会有这样的需求,比如某些是系统资源目录,该目录下的资源也需要像App_Data目录一样禁止访问 ...

  6. spring3: 访问Resource — ResourceLoader/ResourceLoaderAware接口

    4.3.1  ResourceLoader接口 ResourceLoader接口用于返回Resource对象:其实现可以看作是一个生产Resource的工厂类. public interface Re ...

  7. 使用springboot 2.0后,静态资源默认路径无法访问

    原因在于:META-INF/resources / resources / static / public 都是spring boot 认为静态资源应该放置的位置,会自动去寻找静态资源 然而,在2.0 ...

  8. node静态资源服务器的搭建----访问本地文件夹(搭建可访问静态文件的服务器)

    我们的目标是实现一个可访问静态文件的服务器,即可以在浏览器访问文件夹和文件,通过点击来查看文件. 1.先创建一个文件夹anydoor,然后在该文件夹里npm init一个package.json文件, ...

  9. 【壹拾壹周】final_review

    项目名:俄罗斯方块 组名:新蜂 组长:武志远 组员:宫成荣 杨柳 谢孝淼 李桥 final Review会议 时间:2016.12.3 会议内容 设想和目标 1.在final阶段发布时的预期目标是什么 ...

随机推荐

  1. C语言中的二维数组和数组指针的那些事

    其实很多人就想弄明白这个定义了的数组指针里面赋值给的a究竟是什么 a是一个列地址,那么*a是什么,*a还是个地址它存储了行地址.如果我们定义一个指针,那他就有点二级指针的味道.因为它存储的是一个地址, ...

  2. JavaScript的循环语句

    JavaScript的循环语句 1.JavaScript的循环语句 (1)for循环语句 - 循环代码块一定的次数: (2)for/in循环语句 - 循环遍历对象的属性: (3)while循环语句 - ...

  3. __declspec关键字详细用法

    __declspec关键字详细用法 __declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和C++语言的ANSI ...

  4. matplotlib

    前导: 安装 numpy http://sourceforge.net/projects/numpy/files/ http://sourceforge.net/projects/numpy/file ...

  5. How to evaluate a transimpedance amplifier (part 1)

    In this blog, I want take a different approach and describe the technical challenge encountered whil ...

  6. OGNL调用静态方法和属性

    ognl的全名是 Object-Graph Navigation Language 表示的是图对象导航语言...我觉得它最厉害的一点是,通过"."来实现对象的导航...下面看他他的 ...

  7. LINQ 简单用法【1】

    LINQ:Language INtegrated Query,语言集成查询. 以下内容演示如何利用LINQ进行增加,修改,删除和查询操作,针对数据库. 首先创建Linq Class. 添加数据库信息, ...

  8. CUBRID学习笔记 36 在net中添加多行记录

    using System.Data.Common; using CUBRID.Data.CUBRIDClient; namespace Sample { class Add_MultipleRows ...

  9. poj 1474 Video Surveillance (半平面交)

    链接:http://poj.org/problem?id=1474 Video Surveillance Time Limit: 1000MS   Memory Limit: 10000K Total ...

  10. .NET/C# 使用Stopwatch测量运行时间

    Stopwatch类:http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch(v=vs.100).aspx 常用属性和 ...