SpringFramework-资源操作

资源操作(Resources)

Resources接口和实现类

UrlResource访问网络资源

可以访问http前缀和file前缀的资源,其中file前缀的资源要放到项目的根目录下

UrlResource resource = new UrlResource(path);

ClassPathResource访问类路径下资源

此处的类路径是指resources目录

ClassPathResource resource = new ClassPathResource(path);
//此处用java中的流操作对文件以单字节进行读取
try{
    InputStream in = resource.getInputStream();
    byte[] b = new byte[1024];
    while(in.read(b)!=-1){
        System.out.println(new String());
    }
}catch(IOException e){
    throw new RuntimeException(e);
}

FileSystemResource访问文件系统资源

此处的path可以是绝对路径也可以是相对路径
当使用相对路径时,此时相对的是项目的根路径

FileSystemResource resource = new FileSystemResource(path);

ResourceLoader接口

ResourceLoader 接口是用于加载资源的策略接口。它定义了一种统一的方式来访问不同类型的资源,例如文件系统、类路径、URL 等。ResourceLoader 接口的主要实现类是 DefaultResourceLoader。ApplicationContext实现类都实现了这个接口,ApplicationContext可以直接获取Resource实例

        ResourceLoader resourceLoader = new DefaultResourceLoader();
        
        // 加载类路径下的资源
        Resource resource = resourceLoader.getResource("classpath:example.txt");
        
        // 检查资源是否存在
        if (resource.exists()) {
            // 处理资源
            System.out.println("Resource found: " + resource.getFilename());
        } else {
            System.out.println("Resource not found");
        } 

ResourceLoaderAware接口

ResourceLoaderAware 接口是一个用于访问 ResourceLoader 的接口,通过实现 ResourceLoaderAware 接口,可以让 Spring 容器在将 ResourceLoader 注入到实现类中时调用相应的方法,从而让实现类获取到 ResourceLoader 实例

public class MyResourceLoader implements ResourceLoaderAware {

    private ResourceLoader resourceLoader;

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

    // 使用 resourceLoader 获取资源的方法
    public void loadResource() {
        // 使用 resourceLoader 获取资源的逻辑
        // 例如 resourceLoader.getResource("classpath:myFile.txt");
    }
}

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

概述

无论以何种方式创建ApplicationContext实例,都需要为ApplicationContext指定配置文件,Spring运行使用一份或多分XML配置文件。当程序创建ApplicationContext实例时,通常页式以Resource的方式来访问配置文件的,所以ApplicationContext完全支持ClassPathResource、FileSystemResource、ServletContextResource等资源访问方式

使用ApplicationContext实现类指定访问策略

创建ApplicationContext对象时通常使用如下实现类

ClassPathXMLApplicationContext:对应使用ClassPathResource进行资源访问
FileSystemXmlApplicationContext:对应使用FileSystemResource进行资源访问
XmlWebApplicationContext:对应使用ServletContextResource进行资源访问

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

使用前缀指定访问策略

classpath::使用classpath:前缀可以从类路径加载文件,这意味着文件应位于类路径下,例如resources文件夹下。

Resource resource = resourceLoader.getResource("classpath:example.txt");

file::使用file:前缀可以从文件系统加载文件,指定文件的绝对路径或相对路径。

Resource resource = resourceLoader.getResource("file:/path/to/file/example.txt");

http:、https::使用http:或https:前缀可以从远程服务器加载文件。

Resource resource = resourceLoader.getResource("http://example.com/example.txt");

ftp::使用ftp:前缀可以从FTP服务器加载文件。

Resource resource = resourceLoader.getResource("ftp://example.com/example.txt");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值