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

推荐订阅源

Scott Helme
Scott Helme
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
S
Securelist
Help Net Security
Help Net Security
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
D
Docker
Simon Willison's Weblog
Simon Willison's Weblog
Microsoft Azure Blog
Microsoft Azure Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
I
Intezer
Spread Privacy
Spread Privacy
WordPress大学
WordPress大学
C
Cisco Blogs
有赞技术团队
有赞技术团队
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
S
Schneier on Security
Know Your Adversary
Know Your Adversary
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
S
SegmentFault 最新的问题
G
GRAHAM CLULEY
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LINUX DO - 热门话题
K
Kaspersky official blog
P
Proofpoint News Feed
P
Palo Alto Networks Blog
Cyberwarzone
Cyberwarzone
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tor Project blog
NISL@THU
NISL@THU
L
LangChain Blog
B
Blog
aimingoo的专栏
aimingoo的专栏
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cisco Talos Blog
Cisco Talos Blog
雷峰网
雷峰网
The Cloudflare Blog
宝玉的分享
宝玉的分享
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
C
Cyber Attacks, Cyber Crime and Cyber Security

CAYZLH

英雄联盟的昨日今天与符文大乱斗 折腾的尽头是极简 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快捷键的使用 SpringBoot文件上传异常处理 SpringBoot封装JedisUtils工具类 SpringBoot自动部署脚本 Spring统一异常返回 利用Github做Maven私服 Redis常用指令 Maven常用指令 使用mysqldump导出数据 SpringBoot动态切换多数据源 Java8的日期处理实践 lambda表达式语法 Stream表达式语法 使用Optional优雅地判空 Android Support vs AndroidX 「Vue」Runtime-Complier和Runtime-only的区别 RecyclerView使用记录 动态设置布局之LayoutInflater Redis分布式锁的几种方案 打造一个舒服的写作环境(Hexo) 深入了解与使用ThreadLocal Maven中dependencyManagement的作用 Redis配置认证密码 nginx简单配置示例 Linux(macOS)换源 Linux搭建Git服务器 Linux配置ssh使用公钥登录远程服务器 Linux下find与exec的使用 Linux排查Java问题工具单 Linux常用命令 MySql数据库优化细节 API签名验证方案 Java集合框架 Redis基础知识总结 Spring事务管理 Redis为什么这么快 SpringBoot实现Jwt单点登录 SpringBoot实现Redis分布式锁 Spring面向切面编程(知识梳理) SpringBoot异步请求和异步调用 Mybatis中用到的几种设计模式 Docker Swarm RocketMQ相关流程图/原理图 SonarQube的安装与使用 宝塔面板部署vue项目 Tomcat的三种接收请求处理方式 Docker知识扫盲 安装Jenkins并用于部署SpringBoot项目 Tomcat单机多实例部署 Maven中Scope的分类 Springboot集成Shiro(前后端分离) Linux搭建frp服务(内网穿透) Redis高逼格指令 Docker私有仓库的搭建与使用 Dockerfile使用介绍 Redis的使用场景 Docker的网络模式
使用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");
});
}