赞
踩
原文网址:SpringBoot--配置文件--active/include--多环境/公共配置_IT利刃出鞘的博客-CSDN博客
说明
本文介绍SpringBoot如何切换多环境(开发、测试、生产)以及如何引入公共的配置文件。
application.yml的spring.profiles.active用于切换多环境(选择目前激活的是哪个环境),spring.profiles.include用于引入公共的配置文件。
application.yml简单配置示例
- # 服务器配置
- server:
- port: 8082
-
- # spring配置
- spring:
- datasource:
- url: jdbc:mysql://localhost:3306/DatebaseName
- username: root
- password: 123
- driverClassName: com.mysql.jdbc.Driver
我们一般将开发(dev),测试(test),生产(prod)的配置写到不同的配置文件里边,运行时通过spring.profiles.active来选择使用哪个配置。
java命令指定参数
法1:使用-D(推荐)
法2:使用--
spring.profiles.active 和 spring.profiles.include 区别
spring.profiles.active 和 spring.profiles.include 有什么区别呢?笔者认为主要是语意上的区别,实际使用效果基本相同。active 是与环境有关的,include 是与环境无关的。
实际使用,只有下边这一处区别:
The properties from spring.profile.include override default properties. The properties from active profiles override spring.profile.include and default properties.
即:spring.profile.include的属性会覆盖默认属性,spring.profiles.active会覆盖spring.profile.include和默认属性。
application.yml
- spring:
- profiles:
- #激活开发环境
- active: dev
-
- spring:
- application:
- name: order
application-dev.yml
- #开发环境配置
- spring:
- profiles: dev
- server:
- port: 8080
application-prod.yml
- #生产环境配置
- spring:
- profiles: prod
- server:
- port: 8082
例如:
application.yml
- 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
上边是文章的部分内容,为便于维护,全文已转移到此网址:SpringBoot-配置文件的active/include-多环境/公共配置 - 自学精灵
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。