Spring Boot学习06--多环境配置

2021/10/24 6:39:40

本文主要是介绍Spring Boot学习06--多环境配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

方式一:使用多个properties文件

定义如下三个用于不同环境的配置文件

1、application-dev.properties    用于开发环境

server.port=8081

 

2、application-test.properties    用于测试环境

server.port=8082

 

3、application-pro.properties    用于生产环境

server.port=8083

 

在主配置文件application.properties中,激活对应的配置文件:

#激活application-dev.properties 配置文件
spring.profiles.active=dev

#激活application-test.properties 配置文件
#spring.profiles.active=test

#激活application-pro.properties 配置文件
#spring.profiles.active=pro

启动端口为:8081

 

方式二:在application.yml文件中定义多个节

server:
  port: 8080
spring:
  profiles:
    active: test
---
server:
  port: 8081
spring:
  config:
    activate:
      on-profile: dev
---
server:
  port: 8082
spring:
  config:
    activate:
      on-profile: test
---
server:
  port: 8083
spring:
  config:
    activate:
      on-profile: pro

启动端口为:8082

 

方式三:使用虚拟机参数配置

 

 

 

这样设置,启动端口是8083

 

方式四:使用命令行参数

对应的命令为:java -jar xxx.jar --spring.profiles.active=dev

这样设置,启动端口是8081(此步设置需要先删除第三步中的虚拟机参数的设置)

 



这篇关于Spring Boot学习06--多环境配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程