Spring boot 如何读取jar包外面的properties文件

先来张target跟目录的文件结构

conf文件夹中存放的是properties文件,

lib文件夹中存放的是各种jar文件,

start.bat是windows命令行的启动文件。

来张properties文件的内容

1. 创建配置文件相关的配置类,每个properties文件对应一个配置类

这里使用了@ConfigurationProperties,

prefix是指properties文件中的前缀, 比如 asdf.name, 前缀就是asdf,  实体类中的name对应着asdf后面的name。

locations是指的properties文件的地址,这里我用了file, 而不是classpath,  使用classpath总是出问题,不晓得为什么。  conf文件夹是因为start命令行是在target根目录,当前运行的目录就是target根目录。

2. 在application类中添加@EnableConfigurationProperties

有几个配置类,就要加几个类。

3. 在要使用的地方导入

4. 可以使用了

原文地址:https://www.cnblogs.com/yfdream/p/6322116.html