1.通过Resource接口获取资源

Resource接口的实现类有:

Resource接口继承了InputStreamSource 接口,InputStreamSource 接口中有一个方法:getInputStream(),所以汇总起来,Resource接口中共有以下方法:

public class ResourceTest {

    /**使用ClassPathResource获取资源**/
@Test
public void TestClassPath() throws IOException{
Resource resource = new ClassPathResource("test.txt"); //判断文件是否存在:
if (resource.exists()) {
System.out.println("文件存在");
} //判断资源文件是否可读
if (resource.isReadable()) {
System.out.println("文件可读");
} //判断当前Resource代表的底层资源是否已经打开
if (resource.isOpen()) {
System.out.println("资源文件已打开");
} System.out.println(resource.getURL());//获取资源所在的URL
System.out.println(resource.getURI());//获取资源所在的URI
resource.getFile();//返回当前资源对应的File。
System.out.println(resource.contentLength());//输出内容长度
System.out.println(resource.lastModified());//返回当前Resource代表的底层资源的最后修改时间。
resource.createRelative("MyFile");//根据资源的相对路径创建新资源。[默认不支持创建相对路径资源]
System.out.println(resource.getFilename());//获取资源文件名
System.out.println(resource.getDescription()); //获取当前资源代表的输入流
if (resource.isReadable()) {
InputStream is = resource.getInputStream();
System.out.println(is);
is.close();
} } /**使用FileSystemResource获取资源**/
@Test
public void TestFileSystem() throws IOException {
Resource resource = new FileSystemResource("D:\\test.txt");
System.out.println(resource.getFilename());
} /**使用UrlResource获取资源**/
@Test
public void TestUrl() throws MalformedURLException{
Resource resource = new UrlResource("http://docs.spring.io/spring/docs/4.0.0.M1/spring-framework-reference/pdf/spring-framework-reference.pdf");
System.out.println(resource.getFilename());
} /**使用ByteArrayResource获取字节数组封装的资源**/
@Test
public void testByteArray() throws IOException {
ByteArrayResource resource = new ByteArrayResource("Hello".getBytes());
System.out.println(resource.getInputStream()); } /**使用InputStreamResource获取输入流封装的资源。针对于输入流的Resource,其getInputStream()方法只能被调用一次。**/
@Test
public void testInputStream() throws Exception {
InputStream is = new FileInputStream("D\\test.txt");
InputStreamResource resource = new InputStreamResource(is);
//对于InputStreamResource而言,其getInputStream()方法只能调用一次,继续调用将抛出异常。
InputStream is2 = resource.getInputStream(); //返回的就是构件时的那个InputStream
System.out.println(is2);
is.close();
}
}

2.通过ResourceLoader接口获取资源

Spring框架为了更方便的获取资源,尽量弱化程序员对各个Resource接口的实现类的感知,定义了另一个ResourceLoader接口。该接口的getResource(String location)方法可以用来获取资源。它的DefaultResourceLoader实现类可以适用于所有的环境,可以返回ClassPathResource、UrlResource等。

ResourceLoader在进行加载资源时需要使用前缀来指定需要加载:“classpath:path”表示返回ClasspathResource,“http://path”和“file:path”表示返回UrlResource资源,如果不加前缀则需要根据当前上下文来决定,DefaultResourceLoader默认实现是加载classpath资源。

       @Test
public void testResourceLoader() { ResourceLoader loader = new DefaultResourceLoader();
Resource resource = loader.getResource("http://www.baidu.com");
System.out.println(resource instanceof UrlResource); //true
resource = loader.getResource("classpath:test.txt");
System.out.println(resource instanceof ClassPathResource); //true
resource = loader.getResource("test.txt");
System.out.println(resource instanceof ClassPathResource); //true }

3.通过ApplicationContext获取资源

所有ApplicationContext实例都实现了ResourceLoader接口,因此我们在使用Spring容器时,可以不去过于计较底层Resource的实现,也不需要自己创建Resource实现类,而是直接使用applicationContext.getResource(),获取到bean容器本身的Resource,进而取到相关的资源信息。

public class MyResource implements ApplicationContextAware {
private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} public void resource() throws IOException {
Resource resource = applicationContext.getResource("file:D:\\test.txt");
System.out.println(resource.getFilename());
System.out.println(resource.contentLength()); } }

配置xml文件:

<bean id="myResource" class="com.spring.test.MyResource"></bean>

测试类:

public class App {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-resource.xml");
MyResource myResource = (MyResource) context.getBean("myResource");
try {
myResource.resource();
} catch (IOException e) {
e.printStackTrace();
} }
}

Spring中Resource(资源)的获取的更多相关文章

  1. Spring中的资源文件框架——Resource

    摘要 Spring4 以后,官方推荐我们使用Java Config来代替applicationContext.xml,声明将Bean交给容器管理. 在Spring Boot中,Java Config的 ...

  2. Spring中对资源的读取支持

    Resource简单介绍 注:所有操作基于配置好的Spring开发环境中. 在Spring中,最为核心的部分就是applicationContext.xml文件,而此配置文件中字符串的功能发挥到了极致 ...

  3. 简说Spring中的资源加载

    声明: 本文若有 任何纰漏.错误,请不吝指正!谢谢! 问题描述 遇到一个关于资源加载的问题,因此简单的记录一下,对Spring资源加载也做一个记录. 问题起因是使用了@PropertySource来进 ...

  4. Spring中@Resource注解报错

    描述:Spring框架中,@Resource注解报错,在书写时没有自动提示 解决方法:因为maven配置文件的pom.xml文件中缺少javax.annotation的依赖,在pom.项目路中加入依赖 ...

  5. Spring中@Resource、@controller注解的含义

    @Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象. Spring确实支 ...

  6. spring中@Resource和@Autowired理解

    一.@Resource的理解 @Resource在bean注入的时候使用,@Resource所属包其实不是spring,而是javax.annotation.Resource,只不过spring支持该 ...

  7. Java开发中关于资源路径获取问题

    描述 在开发中经常会读取配置文件,在Web开发中大多数都是在项目路径下.核心的API类或者是Controller异或是jsp页面等,基本都是基于web应用的相对路径,很少去操作绝对路径,但是在客户端. ...

  8. Spring中的资源加载

    大家也都知道JDK的类加载器:BootStrap ClassLoader.ExtenSion ClassLoader.Application ClassLoader:也使用了双亲委派模型,主要是为了防 ...

  9. Spring中@Resource与@Autowired、@Qualifier的用法与区别

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...

随机推荐

  1. C语言:源代码兼容WINDOWS LINUX

    使用库函数之前,应该用#include引入对应的头文件.这种以#号开头的命令称为预处理命令.C语言源文件要经过编译.链接才能生成可执行程序:1) 编译(Compile)会将源文件(.c文件)转换为目标 ...

  2. Docker安装和常用配置【Linux】

    Linux下安装配置docker 安装指南:https://developer.aliyun.com/article/110806 一.配置国内镜像源 1.1 设置国内阿里巴巴下载源 [root@lo ...

  3. odoo14里面给下载PDF附件加水印

    依赖包:pip install reportlab Odoo 中附件的下载会经过 ir.http 的 def binary_content() 方法获取附件内容等必要信息, 所以我们需要继承 ir.h ...

  4. 构建后端第4篇之---spring 源码阅读构建环境

    解决 IDEA 创建 Gradle 项目没有src目录问题 in new model named zyt-study   root dir there are  a build.gradle plug ...

  5. php 随机生成字符串

    private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJK ...

  6. 论文笔记:(2019)GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature of Point Cloud

    目录 摘要 一.引言 二.相关工作 基于体素网格的特征学习 直接从非结构化点云中学习特征 从多视图模型中学习特征 几何深度学习的学习特征 三.GAPNet架构 3.1 GAPLayer 局部结构表示 ...

  7. Jenkins插件安装失败

    插件安装失败 通常要下载国外的软件插件之类的时候,链接到国外会太慢或者被墙,这就需要我们去换镜像源 修改配置文件 我们在jenkins里更改升级站点的url后 若安装插件时还是一直卡在"安装 ...

  8. 用AutoHotkey的热字串功能启动常用电脑程序软件 Version 2 Build 20191214

    ; 用AutoHotkey的热字串功能启动常用电脑程序软件 Version 2 Build 20191214 ; 电脑上的快捷键太多了,记都记不住,容易冲突和搞混,所以做了个热字串启动; 用法:运行此 ...

  9. C++ 封装类 2 设计一个学生类 属性有姓名学号 可以给姓名 和学号赋值 可以显示学生的姓名和学号

    1 //设计一个学生类 属性有姓名学号 可以给姓名 和学号赋值 可以显示学生的姓名和学号 2 #include <iostream> 3 #include<string> 4 ...

  10. 方法对了,你做1年Android开发能顶别人做10年

    前几天后台有读者问我这样的问题.他在一家互联网公司工作3年了,每天都很忙,事情又多又杂. 本想着学习多一些东西也不是坏事,可到头来一无所获,什么都没学会,满腔的热情也被消磨得差不多. 三天两头动辞职的 ...