javascript
Spring Boot中的Profile文件
目錄
?
?
理論
演示
?
理論
Profile是Spring對不同環境提供不同配置功能的支持,可以通過激活、指定參數等方式快速切換環境:
1. 多profile文件形式:
格式:application-{profile}.properties
application-dev.properties、application-prod.properties
2. yml中文件塊模式:
server:port: 8085 spring:profiles:active: aaa ---server:port: 8089 spring:profiles: aaa---server:port: 8083 spring:profiles: prod3. 激活方式:
? ? ? ? ? 命令行:--spring.profiles.active=dev
? ? ? ? ? 配置文件:spring.profiles.active=dev
? ? ? ? ? jvm參數:Dspring.profiles.active=dev
?
?
?
演示
首先演示properties文件:
程序結構如下:
源碼如下:
ProfileApplication.java
package com.profiledemo.profile;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class ProfileApplication {public static void main(String[] args) {SpringApplication.run(ProfileApplication.class, args);}}application.properties
server.port=8080 spring.profiles.active=prodapplication-dev.properties
server.port=8081application-prod.properties
server.port=80運行截圖如下:
可見prod生效了!
?
使用yml文件:把所有的properties的都注釋掉。
程序結構如下:
application.yml
server:port: 8085 spring:profiles:active: aaa ---server:port: 8089 spring:profiles: aaa---server:port: 8083 spring:profiles: prod運行截圖如下:
?
通過虛擬機和指定激活profile位置
profile位置:
--spring.profiles.active=dev
虛擬機:
-Dspring.profiles.active=dev
?
打包后可以使用命令行的方式操作:
java -jar xxx.jar --spring.profiles.active=dev
總結
以上是生活随笔為你收集整理的Spring Boot中的Profile文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leaflet文档阅读笔记-Zoom l
- 下一篇: Web笔记-session盗用安全问题(