Spring Boot 多环境配置

       在项目的开发过程中,有些配置信息在开发(dev)、测试(test)、预发布(uat)或者生产(prod)等不同环境中可能是不同的,例如数据库连接、redis的配置等等。如何在不同环境中实现配置的便捷切换呢?Spring提供了profiles机制来解决这个问题,下面分析在Spring Boot中如何使用Profiles功能完成多环境配置。
 
       在Spring Boot中多环境配置文件名需要符合 application-{profile}.properties 的格式,这里的**{profile}**对应的是你的环境标识。例如:
    • application-dev.properties — 这是开发环境
    • application-test.properties — 这是测试环境
        我们可以将与环境无关的配置放到application.properties中,而与环境有关的配置放到各个application-{profile}.properties文件中。程序启动时,会默认加载application.properties中的配置。我们想要使用对应的环境,只需要在application.properties中使用spring.profiles.active属性来设置,其值对应于{profile},这里就是指dev、test等。例如:spring.profiles.active=test就会加载application-test.properties中的内容。
 
原文地址:https://www.cnblogs.com/east7/p/13189654.html