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.txt
class 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.txt
file [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 中 ? :true
myTemplate.txt
class path resource [myTemplate.txt]

        

啦啦啦

啦啦啦

啦啦啦

8 -- 深入使用Spring -- 3...2 ResouceLoader 接口和 ResourceLoaderAware 接口的更多相关文章

  1. Spring源码分析——BeanFactory体系之接口详细分析

    Spring的BeanFactory的继承体系堪称经典.这是众所周知的!作为Java程序员,不能错过! 前面的博文分析了Spring的Resource资源类Resouce.今天开始分析Spring的I ...

  2. Spring IOC三种注入方式(接口注入、setter注入、构造器注入)(摘抄)

    IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转, Spring 框架的核心基于控制反转原理. 什么是控制反转?控制反转是一种将组件依赖关系的创建和管理置于程序外部的技 ...

  3. Spring Resource之ResourceLoaderAware接口

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

  4. 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)

    ApplicationContextAware 接口的作用 先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述:   即是说,当一个类实现了这个接口之 ...

  5. Spring InitializingBean 接口以及Aware接口实现的原理

    关于Spring InitializingBean 接口以及Aware接口实现的其实都在 第11步中: finishBeanFactoryInitialization() 方法中完成了3部分的内容: ...

  6. Spring Boot REST(一)核心接口

    Spring Boot REST(一)核心接口 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) SpringBoot RE ...

  7. Spring AOP 自定义注解获取http接口及WebService接口入参和出参

    注解方法实现过程中可以采用如下获取方式:—以下为例  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHo ...

  8. java web项目(spring项目)中集成webservice ,实现对外开放接口

    什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...

  9. spring boot 2 集成JWT实现api接口认证

    JSON Web Token(JWT)是目前流行的跨域身份验证解决方案.官网:https://jwt.io/本文使用spring boot 2 集成JWT实现api接口验证. 一.JWT的数据结构 J ...

随机推荐

  1. 如何在ROS中使用PCL—数据格式(1)

    在ROS中点云的数据类型 在ROS中表示点云的数据结构有: sensor_msgs::PointCloud      sensor_msgs::PointCloud2     pcl::PointCl ...

  2. OpenResty最佳实践

    https://moonbingbing.gitbooks.io/openresty-best-practices/content/

  3. 关于Unity中RectTransform和Transform

    以前一直以为在Inspector面板上的是Transform,后来才发现原来2D是RectTransform,3D是Transform 3D面板上显示的是位置坐标组件Transform,2D面板上显示 ...

  4. ORACLE 数据库优化原则

    ORACLE 数据库优化原则 一.SQL语句用大写的: 因为Oracle总是先解析SQL语句,把小写的字母转换成大写的再厉行. 二.避免在索引列上利用NOT等闲 我们要避免在索引列上利用NOT, NO ...

  5. 【转】【CentOS】【Python】Centos7安装Python3的方法

    由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_centos Python-3.6.2]# ...

  6. Unity入门教程(下)

    一.概要 在 Unity入门教程(上) 中我们创建了一个游戏项目,并且创建了玩家角色和小球这些游戏对象,还通过添加游戏脚本实现了小方块的弹跳.虽然功能比较简单,但是完整地表现了使用Unity开发游戏的 ...

  7. Oracle EM错误,java.lang.Exception: Exception in sending Request :: null 分类: Oracle 2015-07-08 21:24 44人阅读 评论(0) 收藏

    操作系统:Win7 64bit Oracle: 10.2.0.1.0 很久没有使用EM了,打开一看,居然报错了,出现java.lang.Exception: Exception in sending ...

  8. bioerl 获取gi号

    代码示例: use Bio::DB::EUtilities; my @ids = qw(CAB02640 EAS10332 YP_250808 NP_623143 P41007); my $facto ...

  9. MAC 查看端口占用

    命令 lsof -i tcp:[port]  (port替换成端口号,比如6379)可以查看该端口被什么程序占用,并显示PID,方便KILL 之后,kill -9  [PID] , kill进程即可 ...

  10. Linux学习笔记(二):实战-根据微服务端口号关闭进程

    前言 现在项目组基本都用Springboot,每个服务占用一个端口号,有时需要选择性的关闭,但在任务管理器上他们的名称都是java.exe,无法区分,这才学以致用. killPort.sh 作用:根据 ...