惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
罗磊的独立博客
M
MIT News - Artificial intelligence
G
Google Developers Blog
V
V2EX
D
Docker
博客园_首页
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
WordPress大学
WordPress大学
T
Tailwind CSS Blog
博客园 - 司徒正美
J
Java Code Geeks
L
LangChain Blog
博客园 - 三生石上(FineUI控件)
B
Blog RSS Feed
博客园 - 【当耐特】
小众软件
小众软件
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - Franky

CAYZLH

Reasonix 开箱:聊聊我装的那些 Skill 收藏一份自驾中国的愿望清单 白夜行·谢幕 英雄联盟的昨日今天与符文大乱斗 折腾的尽头是极简 一个 `data.` 引发的血案——Redis 连接 localhost 问题排查全记录 MacBook上备忘录APP的快捷键和手势 让Google屏蔽某些搜索结果 WSL自定义安装Ubuntu typora自动上传图片配置 自建giscus服务 Windows下结束指定端口的进程 批量修改maven多模块版本号 使用winsw部署SpringBoot项目 Gson简易指南 docker搭建Consul集群 使用Docker部署SpringBoot项目 Docker使用redis镜像 Docker使用rabbitmq Docker使用zookeeper Docker使用MySQL Dockerfile常用指令 Docker免sudo操作 雷鸟电视去广告 使用ADB卸载MIUI系统应用 利用GitHub做图床 将网站变成灰色 Github上传大文件 Android远程调试命令adb vscode快捷键的使用
使用Spring读取文件的几种方式
Ant丶 · 2022-10-15 · via CAYZLH

概述

在业务开发中经常有可能读取一些自定义配置或者文件。比如说公私钥文件、一些固定的词典文件之类的,这一类统称为资源(Resource)。Spring自带有资源加载功能,甚至还有非常便利的方法将读取的内容注入Spring bean。

整理网络上相关方法与自身实践的实践

通过Resource接口读取文件

我们可以使用org.springframework.core.io.Resource接口简化资源文件的定位。Spring帮助我们使用资源加载器查找和读取资源,资源加载器根据提供的路径决定选择哪个Resource实现。

使用Resource的实现类

org.springframework.core.io.Resource接口常用的有两个实现类:

  • org.springframework.core.io.ClassPathResource

    用来加载classpath下的资源,直接读取springboot 配置文件 application.properties,其中已经写入了一个配置 server.port=8080

    @Test
    public void classPathResourceTest() throws IOException {
    Resource resource = new ClassPathResource("application.properties");
    InputStream inputStream = resource.getInputStream();
    Properties properties = new Properties();
    properties.load(inputStream);
    properties.forEach((o, o2) -> {
    Assertions.assertThat(o).isEqualTo("server.address");
    Assertions.assertThat(o2).isEqualTo("8080");
    });
    }
  • org.springframework.core.io.FileSystemResource

    用来加载系统文件,通常通过文件的绝对值或者相对路径来读取。需要文件的路径。

    @Test
    public void fileResourceTest() throws IOException {
    String path = "/workspaces/springboot-demo/src/main/resources/application.properties";
    FileSystemResource resource = new FileSystemResource(path);
    InputStream inputStream = resource.getInputStream();
    Properties properties = new Properties();
    properties.load(inputStream);
    properties.forEach((o,o2) -> {
    Assertions.assertThat(o).isEqualTo("server.adderss");
    Assertions.assertThat(o2).isEqualTo("8080");
    });
    }

使用ResourceLoader

使用ResourceLoader可以实现延迟加载:

@Test
public void resourceLoaderTest() throws IOException {
ResourceLoader resourceLoader = new DefaultResourceLoader();
String location = "application.properties";
Resource resource = resourceLoader.getResource(location);
InputStream is = resource.getInputStream();
Properties properties = new Properties();
properties.load(is);
properties.forEach((o,o2) -> {
Assertions.assertThat(o).isEqualTo("server.adderss");
Assertions.assertThat(o2).isEqualTo("8080");
});
}

可以使用@AutowiredResourceLoader注入为bean.

使用@Value注解

@Value("classpath:application.properties")
private Resource resource;

@Test
public void resourceLoader() throws IOException {
InputStream inputStream = resource.getInputStream;
Properties properties = new Properties();
properties.load(inputStream);

properties.forEach((o,o2) -> {
Assertions.assertThat(o).isEqualTo("server.adderss");
Assertions.assertThat(o2).isEqualTo("8080");
});
}