Springboot 多环境配置、${key:default_value}的作用与使用

多环境配置

方式一

创建多个配置文件

application.yml #主配置文件
application-dev.yml #开发环境的配置
application-prod.yml #生产环境的配置
application-test.yml #测试环境的配置
# application.yml
spring:
profiles:
active: dev #激活使用哪个配置文件

方式二:

单个yml文件

#激活dev环境配置
spring:
profiles.active: dev
# 开发环境配置
spring:
profiles: dev
datasource:
url: jdbc:mysql://127.0.0.1:3306/dev?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
server:
port: 8080
# 测试环境配置
spring:
profiles: test
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: test
driver-class-name: com.mysql.jdbc.Driver
server:
port: 8081
# 生产环境配置
spring:
profiles: prod
datasource:
url: jdbc:mysql://localhost:3306/prod?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: prod
driver-class-name: com.mysql.jdbc.Driver
server:
port: 8082

配置默认的profile为dev,其他环境可以通过指定启动参数来使用不同的profile,比如:
测试环境:java -jar 项目.jar --spring.profiles.active=test
java -jar 项目.jar --spring.profiles.active=prod

${key:default_value}的作用与使用

一般写法

server:
port: 8080

${key:default_value}写法

server:
port: ${SERVER_PORT:8080}

key:default_value 在启动的时候命令行传入-DSERVER_PORT=xxx,没有-DSERVER_PORT=xxx ,则取默认值为8080

posted @   一个小笨蛋  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示