springboot配置之Profile多环境支持
Profile是spring对不同环境提供不同配置功能的支持,可以通过激活、指定参数等方式快速切换环境。
- 多profile文件格式:
- 格式:appilication-[profile].properties
application-dev.properties、appilication-prod.properties - 多profile文档块模式
- 激活方式
- 命令行:--spring.profiles.active=dev
- 配置文件:spring.profiles.active=dev
- jvm参数:-Dspring.profiles.active=dev
说明:
第一种方式:
resources下有以下文件:
application.properties
server.port=8080
spring.profiles.active=dev
application-dev.propertites
server.port=8081
application-prod.properties
server.port=8082
这时我们启动springboot:确实切换到了application-dev环境
Tomcat started on port(s): 8081 (http) with context path ''
第二种方式:我们注释掉上述三个文件中的内容,并在application.yml中进行编写:
server: port: 8080 spring: profiles: active: prod --- server: port: 8081 spring: profiles: dev --- server: port: 8082 spring: profiles: prod
使用---可以区分环境块。并可以在主环境块中指定要使用的环境,启动springboot之后:
Tomcat started on port(s): 8081 (http) with context path ''
第三种方式:点击edit Configurations
(1)使用命令行参数
启动springboot:
Tomcat started on port(s): 8082 (http) with context path ''
(2) 使用虚拟机参数
Tomcat started on port(s): 8081 (http) with context path ''
当虚拟机参数和命令行参数都存在时:命令行参数的优先级比虚拟机参数优先级要高
(3)在运行jar包时:java -jar xxx.jar --spring.profiles.active=dev
分类:
springboot
标签:
springboot
· 突破Excel百万数据导出瓶颈:全链路优化实战指南
· 如何把ASP.NET Core WebApi打造成Mcp Server
· Linux系列:如何用perf跟踪.NET程序的mmap泄露
· 日常问题排查-空闲一段时间再请求就超时
· Java虚拟机代码是如何一步一步变复杂且难以理解的?
· 在 .NET 中使用 Sqids 快速的为数字 ID 披上神秘短串,轻松隐藏敏感数字!
· c# 批量注入示例代码
· c#开发完整的Socks5代理客户端与服务端——客户端(已完结)
· 【Uber 面试真题】SQL :每个星期连续5星评价最多的司机
· .net core workflow流程定义