博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
8 -- 深入使用Spring -- 3...2 ResouceLoader 接口和 ResourceLoaderAware 接口
阅读量:7220 次
发布时间:2019-06-29

本文共 4446 字,大约阅读时间需要 14 分钟。

      8.3.2 ResouceLoader 接口和 ResourceLoaderAware 接口

        Spring 提供如下两个标志性接口:

          ⊙ ResourceLoader : 该接口实现类的实例可以获得一个Resource实例。

          ⊙ ResourceLoaderAware : 该接口实现类的实例将获得一个ResourceLoader的引用。

        在ResourceLoader接口里有如下方法:

          ⊙ Resource getResource(String location) : 该接口仅包含这个方法,该方法用于返回一个Resource实例。ApplicationContext的实现类都实现ResourceLoader接口,因此ApplicationContext可用于直接获取Resource实例。

        Class : ClassPathXmlApplicationContext

package edu.pri.lime._8_3_2.main;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.core.io.Resource;public class lll {    public static void main(String[] args) {        ApplicationContext ctx = new ClassPathXmlApplicationContext();//        通过ApplicationContext访问资源//        ApplicationContext实例获取Resource实例时,默认采用与ApplicationContext相同的资源访问策略        Resource res = ctx.getResource("myTemplate.txt");    }}

        Console : 

myTemplate.txtclass path resource [myTemplate.txt]

         Class : FileSystemApplicationContext

package edu.pri.lime._8_3_2.main;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.core.io.Resource;public class FileSystemXML {    public static void main(String[] args) {        ApplicationContext ctx = new FileSystemXmlApplicationContext();        Resource res = ctx.getResource("myTemplate.txt");        System.out.println(res.getFilename());        System.out.println(res.getDescription());    }}

        Console : 

myTemplate.txtfile [E:\Users\Administrator\workspace\lime\myTemplate.txt]

        Spring将采用和ApplicationContext相同的策略来访问资源。也就是说,如果ApplicationContext是FileSystemXmlApplicationContext,res就是FileSystemResource实例;如果ApplicationContext是ClassPathXmlApplicationContext,res就是ClassPathResource实例;如果ApplicationContext是XmlWebApplicationContext,res就是ServletContextResource实例。

        当Spring应用需要进行资源访问时,实际上并不需要直接使用Resource实现类,而是调用ResourceLoader实例的getResource()方法来获得资源。ReosurceLoader将会负责选择Reosurce实现类,也就是确定具体的资源访问策略,从而将应用程序和具体的资源访问策略分离开来,这就是典型的策略模式。

        使用ApplicationContext访问资源时,可通过不同前缀指定强制使用指定的ClassPathResource、FileSystemResource等实现类。

Resource res = ctx.getResource("calsspath:bean.xml");        Resrouce res = ctx.getResource("file:bean.xml");        Resource res = ctx.getResource("http://localhost:8080/beans.xml");

        ResourceLoaderAware完全类似于Spring提供的BeanFactoryAware、BeanNameAware接口,ResourceLoaderAware接口也提供了一个setResourceLoader()方法,该方法将由Spring容器负责调用,Spring容器会将一个ResourceLoader对象作为该方法的参数传入。

        如果把实现ResourceLoaderAware接口的Bean类部署在Spring容器中,Spring容器会将自身当成ResourceLoader作为setResourceLoader()方法的参数传入。由于ApplicationContext的实现类都实现了ResourceLoader接口,Spring容器自身完全可作为ResorceLoader使用。

        Class : TestBean

package edu.pri.lime._8_3_2.bean.impl;import org.springframework.context.ResourceLoaderAware;import org.springframework.core.io.ResourceLoader;public class TestBean implements ResourceLoaderAware{    private ResourceLoader resourceLoader;    //    实现ResourceLoaderAware接口必须实现的方法//    如果把该Bean部署在Spring容器中,该方法将会有Spring容器负责调用。//    SPring容器调用该方法时,Spring会将自身作为参数传给该方法。    public void setResourceLoader(ResourceLoader resourceLoader) {        this.resourceLoader = resourceLoader;    }    //    返回ResourceLoader对象的应用    public ResourceLoader getResourceLoader(){        return this.resourceLoader;    }}

        Class : SpringTest

package edu.pri.lime._8_3_2.bean.main;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.core.io.Resource;import org.springframework.core.io.ResourceLoader;import edu.pri.lime._8_3_2.bean.impl.TestBean;public class SpringTest {    public static void main(String[] args) {        ApplicationContext ctx = new ClassPathXmlApplicationContext("app_8_3_3.xml");        TestBean testBean = ctx.getBean("testBean",TestBean.class);        ResourceLoader resourceLoader = testBean.getResourceLoader();        System.out.println("Spring容器将自身注入到ResourceLoaderAware Bean 中 ? :" + (resourceLoader == ctx));        Resource resource = resourceLoader.getResource("myTemplate.txt");        System.out.println(resource.getFilename());        System.out.println(resource.getDescription());    }}

        Console : 

Spring容器将自身注入到ResourceLoaderAware Bean 中 ? :truemyTemplate.txtclass path resource [myTemplate.txt]

        

啦啦啦

啦啦啦

啦啦啦

转载地址:http://kkhym.baihongyu.com/

你可能感兴趣的文章
基于Canvas,纯Javascript图表库ECharts
查看>>
A Tour of the Dart Language(译文):一Dart基础
查看>>
SVN 以及自动部署配置
查看>>
NoDom软件首页和文档页面无法访问的问题
查看>>
php上传文件,通过curl上传到远程服务器
查看>>
使用 Clozure CL 在 Cocoa 窗口组件中实现显示中文字符串的简单方法
查看>>
UML关系(泛化,实现,依赖,关联(聚合,组合))
查看>>
Servlet生命周期
查看>>
OpenWrt的ipk包安装
查看>>
超实用:开源软件Hyperic 监控 SQL Server
查看>>
在idea中使用tomcat遇到的问题
查看>>
完整部署CentOS7.2+OpenStack+kvm 云平台环境
查看>>
iOS开发证书都显示“此证书的签发者无效”,更新WWDR Certificate证书后还是显示无效...
查看>>
android多语言与国际化
查看>>
ThinkPHP 调用编辑器
查看>>
多文件上传,并生成缩略图
查看>>
sprintf函数分析
查看>>
redis源码分析-1 字符串处理
查看>>
return 、continue和break的区别
查看>>
RabbitMQ中的基本概念
查看>>