资源操作:Resources

Java的标准java.net.URL类和各种URL前缀的标准处理程序无法满足所有对low-level资源访问,比如:没有标准化的URL实现可用于访问需要从类路径或相对于ServletContext获取的资源。并且缺少某些Spring所需要的功能,例如检测某资源是否存在等。而Spring的Resource声明了访问low-level资源的能力。

Resource接口:

针对不同的场景,进行资源访问。用于抽象对低级资源的访问。位于org.springframework.core.io中。比如说取类路径、网络路径、文件系统资源等。

Resource的实现类:Resource是个接口,而接口中包含很多类方法,类方法通过继承接口重写来实现。

UrlResource类方法

用来访问网络资源,它支持URL的绝对路径。

http:------该前缀用于访问基于HTTP协议的网络资源。

ftp:-------该前缀用于访问FTP协议的网络资源

file:-----该前缀用于从文件系统中读取资源

实验:访问基于HTTP协议的网络资源

package com.it.spring6.resource;

import org.springframework.core.io.UrlResource;

import java.io.IOException;
import java.net.MalformedURLException;

//演示UrlResource访问网络资源
public class UrlResourceDemo {

    public static void main(String[] args) {
        //http前缀
        loadUrlResource("http://www.baidu.com");

        //file前缀
        loadUrlResource("file:1.txt"); //1.txt需要放到项目的根路径下

    }
    //访问前缀http、file
    public static void loadUrlResource(String path){

        try {
            //创建Resource实现类对象UrlResource
            UrlResource url = new UrlResource(path);

            //获取资源信息
            System.out.println(url.getFilename());
            System.out.println(url.getURI());
            System.out.println(url.getDescription());
            System.out.println(url.getInputStream().read());

        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //获取资源信息

    }
}

ClassPathResource访问类路径下资源

ClassPathResource用来访问类加载路径下的资源,相对于其他的Resource实现类,其主要优势是方便访问类加载路径里的资源,尤其对于Web应用,ClassPathResource可自动搜索对于classes下的资源文件,无需使用绝对路径访问

在Resources文件下建一个文件,最终通过编译后,会放到classes文件夹下

package com.it.spring6.resource;

import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;

//访问类路径下资源
public class ClassPathResourceDemo {


    public static void loadClasspathResource(String path){
        //创建对象ClassPathResource
        ClassPathResource classPathResource = new ClassPathResource(path);

        System.out.println(classPathResource.getFilename());
        System.out.println(classPathResource.getDescription());
        //获取文件内容
        try {
            InputStream in = classPathResource.getInputStream();
            byte[] bytes = new byte[1024];
            while (in.read(bytes)!=-1){
                System.out.println(new String(bytes));
            }
            //这段代码创建了一个大小为1024的字节数组bytes用于存储从输入流in中读取的数据。
            // 然后通过循环不断从输入流中读取数据到bytes数组中,直到读取的数据长度为-1(即达到流的末尾)。
            // 每次读取后将bytes数组转换为字符串并输出到控制台。这段代码的作用是从输入流中读取数据并逐行输出到控制台。
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        loadClasspathResource("1.txt");
    }

}

输出: 

FileSystemResource实现类:

Spring提供的FileSystemResource类用于访问文件系统资源,例如访问C:\或D:\中的文件

package com.it.spring6.resource;


import org.springframework.core.io.FileSystemResource;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

//访问系统资源
public class FileSystemResourceDemo {
    public static void main(String[] args) {
        //相对路径和绝对路径都支持
        //绝对路径
        loadFileResource("D:\\qqa.txt");
        //相对路径
        loadFileResource("1.txt");
    }
    public static void loadFileResource(String path){
        //创建对象
        FileSystemResource fileSystemResource = new FileSystemResource(path);

        System.out.println(fileSystemResource.getFilename());
        System.out.println(fileSystemResource.getDescription());
        try {
            InputStream inputStream = fileSystemResource.getInputStream();
            byte[] bytes = new byte[1024];
            while (inputStream.read(bytes)!=-1){
                System.out.println(new String(bytes));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}

 输出:

SevletContextResource实现类:

在Web中有使用,它解释相关Web应用程序根目录中的相对路径

InputStreamResourcce实现类:

InputStreamResourcce是给定的输入流InputStream的Resource实现

ByteArrayResource实现类:

字节数组的Resource实现类

Resource类图:

 图片来自尚硅谷教育

ResourceLoader接口:该接口实现类可以获得一个Resource实例。作用:可以得到Resource对象

在ResourceLoader接口有如下方法:

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

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

package com.it.spring6.resourceloader;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.Resource;

public class ResourceLoaderDemo1 {
    @Test
    public void demo1(){
        ApplicationContext context = new ClassPathXmlApplicationContext();
        Resource resource = context.getResource("1.txt");
        System.out.println(resource.getFilename());
    }
    @Test
    public void demo2(){
        ApplicationContext context = new FileSystemXmlApplicationContext();
        Resource resource = context.getResource("1.txt");
        System.out.println(resource.getFilename());
    }
}

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

ResourceLoaderAware接口实现类的实例将获得一个ResourceLoader的引用, ResourceLoaderAware提供了一个setResourceLoader()方法,该方法由Spring容器负责调用,Spring容器会将一个ResourceLoader对象作为该 方法的参数传入。

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

 TestBean.java

package com.it.spring6.resourceloaderaware;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;

public class TestBean implements ResourceLoaderAware {

    private ResourceLoader resourceLoader;
    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public ResourceLoader getResourceLoader() {
        return this.resourceLoader;
    }
}

 TestDemo.java

package com.it.spring6.resourceloaderaware;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ResourceLoader;

public class TestDemo {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        TestBean testBean = context.getBean("testBean", TestBean.class);
        ResourceLoader resourceLoader = testBean.getResourceLoader();

        System.out.println(context == resourceLoader);
    }
}

使用Resource作为属性:

前面介绍了Spring提供的资源访问策略,但这些依赖策略要么需要使用Resource实现类,要么需要使用ApplicationContext来获取资源。实际上,当应用程序中的Bean需要访问资源时,Spring有更好的解决方法:直接利用依赖注入。从这个意义上来看,Spring框架不仅充分利用了策略模式来简化资源访问,而且还将策略模式和IoC进行了充分地结合,最大程度地简化了Spring资源访问。

归纳起来,如果Bean实例需要访问资源,有如下两种解决方案:

  • 代码中获取Resource实例
  • 使用依赖注入

 对于第一种方式,当程序获取Resource实例时,总需要提供Resource所在的位置,不管通过FileSystemResource创建实例,还是通过ClassPathResource创建实例,或者通过ApplicationContext的getResource()方法获取实例,都需要提供资源位置。这意味着:资源所在的物理位置将被耦合到代码中,如果资源位置发生改变,则必须改写程序。因此,通常建议采取第二种方法,让Spring为Bean实例依赖注入资源。

ResourceBean.java

package com.it.spring6.di;

import org.springframework.core.io.Resource;

public class ResourceBean {
    private Resource resource;

    public Resource getResource() {
        return resource;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }
    public void parse(){
        System.out.println(resource.getFilename());
        System.out.println(resource.getDescription());
    }
}

beans.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="resourceBean" class="com.it.spring6.di.ResourceBean">
        <property name="resource" value="classpath:1.txt"></property>
    </bean>
</beans>

 TestBean.java

package com.it.spring6.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBean {
    public static void main(String[] args) {
        ApplicationContext context
                = new ClassPathXmlApplicationContext("beans.xml");
        ResourceBean resourceBean = context.getBean(ResourceBean.class);
        resourceBean.parse();
    }
}

 

应用程序上下文和资源路径:

不管以怎样的方式创建ApplicationContext实例,都需要为ApplicationContext指定配置文件,Spring允许使用一份或多分XML配置文件。当程序创建ApplicationContext实例时,通常也是以Resource的方式来访问配置文件的,所以ApplicationContext完全支持ClassPathResource、ServletContextResource等资源访问模式。

ApplicationContext确定资源访问策略通常有两种方法:

(1)使用ApplicationContext实现类指定访问策略。

(2)使用前缀指定访问策略。

ApplicationContext实现类指定访问策略

创建ApplciationContext对象时,通常可以使用如下实现类:

  1. ClassPathXmlApplicationContext:对应使用ClassPathResource进行资源访问
  2. FileSystemXmlApplicationContext:对应使用FileSystemResource进行资源访问。
  3. XmlWebApplicationContext:对应使用ServletContextResource进行资源访问。

当使用ApplicationContext的不同实现类时,就意为着Spring使用相应的资源访问策略。 

 使用前缀指定访问策略

1.classpath*:前缀提供了加载多个XML配置文件的能力,当使用classpath*:前缀来指定XML配置文件时,系统将搜索类加载路径,找到所有与文件名匹配的文件,分别加载文件中的配置定义,最后合并成一个ApplicationContext。

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*bean.xml");
System.out.println(ctx);

当使用classpath*前缀时,Spring将会搜索类加载路径下所有满足该规则的配置文件。

2.如果不是采用classpath*:前缀,而是改为使用classpath:前缀,Spring则只加载第一个符合条件的XML文件 

public class TestDemo {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:Bean.xml");
        //"classpath:beans.xml" 通过classpath前缀去强制搜索类加载路径下的beans.xml文件
        Resource resource = context.getResource("1.txt");
        System.out.println(resource.getDescription());
    }
}

注意:

classpath*:前缀仅对ApplicationContext有效。实际情况是,创建ApplicationContext时,分别访问多个配置文件(通过ClassLoader的getResource方法实现)。因此,classpath*:前缀不可用于Resource。

3.一次性加载多个配置文件的方式:指定配置文件时使用通配符(查询bean开头的文件 )

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean*.xml");

Spring允许将classpath*:前缀和通配符结合使用:

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:bean*.xml");

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值