原文网址:的多环境与公共配置_IT利刃出鞘的博客-****博客
简介
说明
本文介绍SpringBoot如何切换多环境(开发、测试、生产)以及如何引入公共的配置文件。
的用于切换多环境(选择目前激活的是哪个环境),用于引入公共的配置文件。
简单配置示例
# 服务器配置
server:
port: 8082
# spring配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/DatebaseName
username: root
password: 123
driverClassName:
多环境(active)
简介
我们一般将开发(dev),测试(test),生产(prod)的配置写到不同的配置文件里边,运行时通过来选择使用哪个配置。
java命令指定参数
法1:使用-D(推荐)
- java -=dev -jar test-1.0.
- -Dxxx=yyy必须在-jar之前
- 此法增加的参数被设置到应用的系统属性中,可通过(“”)获取
法2:使用--
- java -jar test-1.0. --=dev
- --xxx=yyy必须在-jar之后
- 此法增加的参数属于命令行参数,会作为SpringBoot启动的main方法的String[] args参数。
- 有时本方法在Windows下无效。
和 区别
和 有什么区别呢?笔者认为主要是语意上的区别,实际使用效果基本相同。active 是与环境有关的,include 是与环境无关的。
实际使用,只有下边这一处区别:
The properties from override default properties. The properties from active profiles override and default properties.
即:的属性会覆盖默认属性,会覆盖和默认属性。
方案1:多个配置文件
spring:
profiles:
#激活开发环境
active: dev
spring:
application:
name: order
#开发环境配置
spring:
profiles: dev
server:
port: 8080
#生产环境配置
spring:
profiles: prod
server:
port: 8082
方案2:使用---
例如:
spring:
profiles:
#激活开发环境
active: dev
spring:
application:
name: order
---
#开发环境配置
spring:
profiles: dev
server:
port: 8080
---
#生产环境配置
spring:
profiles: prod
server:
port: 8082
新版本(SpringBoot2.4.2及之后)写法:
spring:
profiles:
#激活开发环境
active: dev
spring:
application:
name: order
---
#开发环境配置
spring:
config:
activate:
on-profile: dev
server:
port: 8080
---
#生产环境配置
spring:
config:
activate:
on-profile: prod
server:
port: 8082
拆出(include)
上边是文章的部分内容,为便于维护,全文已转移到此网址:SpringBoot-配置文件的active/include-多环境/公共配置 - 自学精灵