基于Spring Boot不同的環(huán)境使用不同的配置方法
spring 多文件配置:
1、properties文件
2、YAML文件
一、properties文件
在 Spring Boot 中, 多環(huán)境配置的文件名需要滿足 application-{profile}.
properties的格式, 其中{profile}對應(yīng)你的環(huán)境標(biāo)識, 如下所示。
• application-dev.properties: 開發(fā)環(huán)境。
• application-test.properties: 測試環(huán)境。
• application-prod.properties: 生產(chǎn)環(huán)境。
至于具體哪個配置文件會被加載, 需要在 app巨ca巨on.properties 文件中通過
spring.profiles.active 屬性來設(shè)置, 其 值 對應(yīng)配置文件中的{profile}值。 如
spring.profiles.active = test就會加載 application-test.properties配置
文件內(nèi)容。
二、YAML文件
server: port: 8080 # 默認(rèn)的profile為dev,其他環(huán)境通過指定啟動參數(shù)使用不同的profile,比如: # 測試環(huán)境:java -jar xxx.jar --spring.profiles.active=test # 生產(chǎn)環(huán)境:java -jar xxx.jar --spring.profiles.active=prod spring: profiles: active: dev #下面這一行務(wù)必不能少,區(qū)分不同配置,而且必須是三個字符"-" --- # 開發(fā)環(huán)境配置 spring: profiles: dev datasource: url: jdbc:mysql://192.168.0.152:3306/aylson?useUnicode=true&characterEncoding=UTF-8&useSSL=false --- # 測試環(huán)境配置 spring: profiles: test datasource: url: jdbc:mysql://192.168.0.152:13306/aylson?useUnicode=true&characterEncoding=UTF-8&useSSL=false --- # 生產(chǎn)環(huán)境配置 spring: profiles: prod datasource: url: jdbc:mysql://192.168.0.152:23306/aylson?useUnicode=true&characterEncoding=UTF-8&useSSL=false
使用方法:
通過指定啟動參數(shù)使用不同的profile,比如:
測試環(huán)境:Java -jar xxx.jar –spring.profiles.active=test
生產(chǎn)環(huán)境:java -jar xxx.jar –spring.profiles.active=prod
以上這篇基于Spring Boot不同的環(huán)境使用不同的配置方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中的ArrayList.trimToSize()方法詳解
這篇文章主要介紹了Java中的ArrayList.trimToSize()方法詳解,前幾天看了Java?ArrayList,沒有明白trimToSize()這個方法是什么意思,所以看了一下源碼并且debug一下自己的一個例子,明白了其中的含義,需要的朋友可以參考下2023-11-11
SpringBoot+Elasticsearch實現(xiàn)數(shù)據(jù)搜索的方法詳解
Elasticsearch是一個基于Lucene的搜索服務(wù)器。它提供了一個分布式多用戶能力的全文搜索引擎,基于RESTful?web接口。本文將利用SpringBoot整合Elasticsearch實現(xiàn)海量級數(shù)據(jù)搜索,需要的可以參考一下2022-05-05
maven倉庫中心mirrors配置多個下載中心(執(zhí)行最快的鏡像)
這篇文章主要介紹了maven倉庫中心mirrors配置多個下載中心(執(zhí)行最快的鏡像),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

