使用Spring Initializr方式创建新项目

SpringBoot基础实战-第四篇-配置-Profile多环境支持

1 properties文件的多环境支持

在resources下创建application-dev.properties

server.port=8182

创建application-prod.properties

server.port=8181

SpringBoot基础实战-第四篇-配置-Profile多环境支持

application.properties的内容添加

server.port=8180
spring.profiles.active=dev

运行SpringbootConfig2Application.java中的main方法:

SpringBoot基础实战-第四篇-配置-Profile多环境支持

 

2 yaml文件的多环境支持

首先将properties文件中的内容注释掉,然后在resources下创建application.yml内容如下

server:
  port: 8081
spring:
  profiles:
    active: prod

---
server:
  port: 8083
spring:
  profiles: dev


---

server:
  port: 8084
spring:
  profiles: prod  #指定属于哪个环境

运行SpringbootConfig2Application.java中的main方法:

SpringBoot基础实战-第四篇-配置-Profile多环境支持

 

3 程序参数方式

--spring.profiles.active=prod

SpringBoot基础实战-第四篇-配置-Profile多环境支持

运行SpringbootConfig2Application.java中的main方法:

SpringBoot基础实战-第四篇-配置-Profile多环境支持

 

4 虚拟机方式

-Dspring.profiles.active=dev

SpringBoot基础实战-第四篇-配置-Profile多环境支持

 

运行SpringbootConfig2Application.java中的main方法:

SpringBoot基础实战-第四篇-配置-Profile多环境支持

5 命令行方式

SpringBoot基础实战-第四篇-配置-Profile多环境支持

在springboot-config2-0.0.1-SNAPSHOT.jar所在的文件夹下启动终端界面,输入命令

java -jar springboot-config2-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

SpringBoot基础实战-第四篇-配置-Profile多环境支持

---------------------------------------------

下载源码

相关文章:

  • 2021-08-20
  • 2022-12-23
  • 2021-09-18
  • 2021-04-13
  • 2021-11-07
  • 2022-12-23
  • 2022-02-15
  • 2021-09-07
猜你喜欢
  • 2021-07-17
  • 2021-10-29
  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2021-08-07
相关资源
相似解决方案