Spring中Profile對不同環境提供不同配置功能的支持,可以通過激活、指定參數等方式快速切換環境。
文件名格式:application-{profile}.properties
可以建立多個properties(yaml)文件來不斷的切換
application-dev.properties
1
|
server.port= 8082 |
application-prod.properties
1
|
server.port= 8083 |
application.properties
1
2
|
server.port= 8081 spring.profiles.active=dev |
文件名格式:application-{profile}.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
server: port: 8082 spring: profiles: active: dev --- spring: profiles: dev server: port: 8083 --- spring: profiles: prod server: port: 8084 --- spring: profiles: default (未指定時默認使用的配置) server: port: 80 |
激活方式:
1
2
3
4
5
6
7
|
yaml中: spring: profiles: active: dev 或 properties中: spring.profiles.active=dev |
運行時:
在打包后運行的時候,添加參數:
1
|
java -jar spring-boot.jar --spring.profiles.active=dev; |
tomcat 中 catalina.bat(.sh中不用“set”) 添加JAVA_OPS。通過設置active選擇不同配置文件:set JAVA_OPTS="-Dspring.profiles.active=test"
web.xml方式
spring.profiles.active prod
標注方式(junit單元測試非常實用)
@ActiveProfiles({“dev”})
到此這篇關于SpringBoot 多Profile使用與切換方式的文章就介紹到這了,更多相關SpringBoot 多Profile使用與切換內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/qq_40742949/article/details/115488008