Springboot 自定义配置文件路径以及多 profile 配置文件
一、什么是classpath
classpath 指的就是 *.java 文件、资源文件等编译后存放的位置,对于 Maven 项目就是指 target/classes 这个路径,只要编译后的文件在这个目录下,springboot 就可以找到,我们这里指的是 Maven 项目, Java Web 项目可能会有区别.
二、自定义springboot配置文件路径
springboot 项目配置文件 application.properties/application.yml 默认放置的位置是 classpath:/、classpath:/config/、file:./、file:./config/ 这4个位置.只要我们编译后的文件位于这 4 个位置,springboot 就可以加载配置文件.但有时候我们需要以环境名称为标识,配置多个环境的配置文件.如下我们需要将配置文件放置在指定的环境标识下.
如上图我们的资源文件的路径已经不再是 springboot 默认的配置路径了,因此 springboot 启动时将无法加载配置文件,需要我们在启动类中手动指定配置文件的加载路径了.
1 2 3 4 5 6 7 8 9 10 11 12 | @SpringBootApplication public class Springboot01Application { public static void main(String[] args) { // springboot 默认的配置文件路径 // String addClassPath = "spring.config.location:classpath:/"; // 自定义的配置文件路径 String addClassPath = "spring.config.additional-location:classpath:/" ; addClassPath += ",classpath:/config/env/" ; new SpringApplicationBuilder(Springboot01Application. class ). properties( "spring.config.name:application" , addClassPath).build().run(args); } } |
三、多 profiles 配置文件的切换
我们平时开发项目的时候,在不同的时期会存在不同的环境配置文件,例如: application-dev.properties、application-sit.properties、application-uat.properties、application-pro.properties,那么我们运行项目的时候如何做到根据当前所处的环境来切换对应的环境配置文件呢?
springboot 为我们提供了切换配置文件的方式,我们在编写配置文件时,文件名可以是: application-{profile}.properties 或者是 application-{profile}.yml
1、application-{profile}.properties方式
一、各环境配置文件如下:
application-dev.properties
1 2 3 4 | person.name=xiaomaomao-dev person.pet=dog-dev person.hobby=LOL-dev person.cleverLevel=nine-dev |
application-sit.properties
1 2 3 4 | person.name=xiaomaomao-sit person.pet=dog-sit person.hobby=LOL-sit person.cleverLevel=nine-sit |
application-uat.properties
1 2 3 4 | person.name=xiaomaomao-uat person.pet=dog-uat person.hobby=LOL-uat person.cleverLevel=nine-uat |
application-pro.properties
1 2 3 4 | person.name=xiaomaomao-pro person.pet=dog-pro person.hobby=LOL-pro person.cleverLevel=nine-pro |
通过 application.properties 来选择需要激活的配置文件
1 | spring.profiles.active=sit |
二、实体类
1 2 3 4 5 6 7 8 9 10 | @Configuration @PropertySource ( "classpath:config/env/application.properties" ) @ConfigurationProperties (prefix = "person" ) // 省略了 get/set public class Person { private String name; private String pet; private String hobby; private String cleverLevel; } |
三、控制器
1 2 3 4 5 6 7 8 9 10 11 | @RestController public class SpringbootProfileController { @Autowired private ApplicationContext ioc; @RequestMapping (value = "/profile" ) public String testProfiles() { Person person = ioc.getBean( "person" , Person. class ); System.out.println(person); return "hello profiles!!!" ; } } |
四、测试结果
2、application.yaml方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | spring: profiles: active: uat # 指定激活哪个配置文件 --- server: port: 8082 spring: profiles: dev --- server: port: 8083 spring: profiles: sit --- server: port: 8084 spring: profiles: uat |
参考:https://blog.csdn.net/m0_37735176/article/details/89085843
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?