在springboot工程中配置文件编写的时候可以使用application-{profile}.properties/yml,默认使用application.properties/yml,例如:

每个环境配置如下:
#application-dev.properties
server.port=8080
#application-test.properties
server.port=8082
#application-dev.properties
server.port=80
我们可以在application.properties/yml指定激活哪个环境的配置文件,我们在发布的时候最终都是读取application.properties/yml中的配置:
#yml
spring:
profiles:
active: @profiles.active@ # 和我们pom.xml中的指定resource配置相同
#properties
spring.profiles.active=dev
pom.xml配置如下:
<dependencies>
<!-- Add typical dependencies for a web application web应用的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId

本文介绍了在SpringBoot项目中如何利用多Profile文件进行环境配置,包括在`application-{profile}.properties`中定义不同环境的配置,通过pom.xml设置或命令行指定激活的Profile。同时,讲解了配置文件的加载优先级和互补机制。在IDEA中,可以通过选择不同的Profile执行打包操作,如dev或prod,以实现跳过测试或生成特定环境的jar包。
最低0.47元/天 解锁文章





