美文网首页
玩转springboot之springboot多环境配置

玩转springboot之springboot多环境配置

作者: 墨线宝 | 来源:发表于2024-06-26 10:14 被阅读0次

springboot多环境配置

springboot对于多环境配置支持多种方式

方式一:多个配置文件

在配置多个环境的配置文件时文件名可以是application-{profile}.properties/yml

默认使用application.properties/yml的配置,然后在默认配置文件中进行环境激活

spring:
  profiles:
    active: dev

方式二:yml支持多文档块

在yml配置文件中可以使用---来进行环境配置分隔,然后在每个文档块来声明环境

server:
  port: 8081
custom:
  name: 张三
spring:
  profiles:
    active: dev

---
spring:
  profiles: dev
custom:
  name: 赵柳
---
spring:
  profiles: prod
custom:
  name: 孙鸥

不同环境加载不同的bean

可以使用@Profile注解,限制加载bean的时机

@Bean
    @Profile("dev")
    public CodeReview codeReview() {
        
        return new CodeReview();
    }

https://zhhll.icu/2021/框架/springboot/基础/3.多环境配置/

本文由mdnice多平台发布

相关文章

网友评论

      本文标题:玩转springboot之springboot多环境配置

      本文链接:https://www.haomeiwen.com/subject/hxfscjtx.html