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

spring3: 访问Resource — ResourceLoader/ResourceLoaderAware接口的更多相关文章

  1. Spring Resource之ResourceLoaderAware接口

    ResourceLoaderAware接口是一个特殊的标记接口,它表示对象需要提供给一个ResourceLoader引用: public interface ResourceLoaderAware { ...

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

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

  3. 8 -- 深入使用Spring -- 3...2 ResouceLoader 接口和 ResourceLoaderAware 接口

    8.3.2 ResouceLoader 接口和 ResourceLoaderAware 接口 Spring 提供如下两个标志性接口: ⊙ ResourceLoader : 该接口实现类的实例可以获得一 ...

  4. 资源 之 4.3 访问Resource(拾壹)

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

  5. JNI的替代者—使用JNA访问Java外部功能接口

    摘自:http://www.cnblogs.com/lanxuezaipiao/p/3635556.html JNI的替代者-使用JNA访问Java外部功能接口 1. JNA简单介绍 先说JNI(Ja ...

  6. JNI的又一替代者—使用JNR访问Java外部函数接口(jnr-ffi)

    1. JNR简单介绍 继上文“JNI的替代者—使用JNA访问Java外部函数接口”,我们知道JNI越来越不受欢迎,JNI是编写Java本地方法以及将Java虚拟机嵌入本地应用程序的标准编程接口.它管理 ...

  7. 【C/C++开发】【Java开发】JNI的替代者—使用JNA访问Java外部功能接口

    JNI的替代者-使用JNA访问Java外部功能接口 1. JNA简单介绍 先说JNI(Java Native Interface)吧,有过不同语言间通信经历的一般都知道,它允许Java代码和其他语言( ...

  8. java 访问 太平洋网ip接口,解决前端js 跨域访问失败问题

    前端 js访问太平洋网IP接口地址,返回结果是403 服务器拒绝处理异常, 于是,想到了使用 服务器端访问,然后再将查询结果返回的前端 这是Java的测试源码,[具体的contronller端源码懒得 ...

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

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

随机推荐

  1. Java中native关键字使用

    native是与C++异构开发的时候用的.java自己开发不会使用

  2. 服务不支持 chkconfig 的解决方法

    系统服务,在chkconfig --add  servername的时候老是提示服务不支持 chkconfig,经过查找,解决办法如下. 示例,auto_run的前三行如下:#!/bin/sh#chk ...

  3. TCP的3次握手和四次挥手,请画图说明流程

    TCP 三次握手 TCP 四次挥手  

  4. 微信js分享朋友圈(一)

    1.绑定域名 先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”. 备注:登录后可在“开发者中心”查看对应的接口权限. 2.引入js文件 <script type=&q ...

  5. Java I/O(一) NIO概述

    基本概念 BIO:是堵塞I/O,无论是磁盘I/O,还是网络I/O,数据在写入OutputStream和InputStream都可能发生堵塞,一旦有堵塞,线程会失去CPU的使用权(堵塞). NIO:简单 ...

  6. Cocos2dx3.1-Android环境搭建初体验

    初玩Cocos2dx,多多包涵. 感觉版本号之间的差异比較大.相对前面的版本号来说.3.X更easy上手.更方便了. 一.安装python.我的python-2.7.3. 配置环境变量 系统变量里:在 ...

  7. hexo+yilia主题博客如何添加图标icon

    1. 先去比特虫网站做icon图标 2. 图片放到hexo/source/img文件夹下 3. 找到hexo\themes\modernist\layout_partial\head.ejs,设置为 ...

  8. Ionic 3 项目的工程目录结构(转载)

    工程目录结构说明如下图

  9. tensorflow 的 tutorial 的卷积神经网络的例子 convolutional.py

    具体的网址在这里: https://github.com/tensorflow/tensorflow/tree/r0.12/tensorflow/models 一个卷积神经网络用于股票分析的例子:  ...

  10. 4 TensorFlow入门之dropout解决overfitting问题

    ------------------------------------ 写在开头:此文参照莫烦python教程(墙裂推荐!!!) ---------------------------------- ...