SpringBoot 使用 spring.profiles.active 来区分不同环境配置

很多时候,我们项目在开发环境和生产环境的配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境,我们要用生产数据库,这时候,我们可以利用 profile 在不同的环境下配置用不同的配置文件或者不同的配置。

Spring Boot 允许你通过命名约定按照一定的格式 (application-{profile}.properties) 来定义多个配置文件,然后通过在 application.properties 通过 spring.profiles.active 来具体激活一个或者多个配置文件,如果没有指定任何 profile 的配置文件,Spring Boot 默认会启动 application-default.properties。

一、具体配置实现方法参考如下
  1. 在 application.properties 文件的同路径下,创建不同的环境参数文件,命名格式为:application-{profile}.properties,其中 {profile} 对应环境标识。

如上图所示,项目共配置了三个不同的环境,分别为:

  • application-dev.properties:开发环境
  • application-test.properties:测试环境
  • application-prod.properties:生产环境

至于哪个具体的配置文件会被加载,需要在 application.properties 文件中通过 spring.profiles.active 属性来设置,其值对应 {profile} 值。

  1. 修改 application.properties 文件内容,指定生效的环境,内容如下。

    spring.profiles.active=dev

spring.profiles.active 取值可为 dev、prod、test。上述配置指定开发环境配置文件有效,即取 application-dev.properties 文件中的相关配置。

  1. 启动时指定生效的 profiles 参数,启动脚本示例如下所示。

    nohup java -Xms3g -Xmx3g -Xmn1g -Xss256k -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/logdisk/gclog/gctest.log -jar test-1.0-SNAPSHOT.jar --spring.profiles.active=prod >/dev/null 2>&1 &

二、配置文件(application.properties)所在目录不同,加载优先级也不同

在 Spring Boot 源码文件(spring-boot-2.1.3.RELEASE.jar)中,org.springframework.boot.context.config 包下的 ConfigFileApplicationListener 类中定义了默认配置文件和默认搜索路径,也可以通过该类中的 setSearchLocations() 和 setSearchNames() 方法来设置其他的搜索位置或者设置其它配置文件名称。

ConfigFileApplicationListener 是一个 ApplicationListener,也是一个 EnvironmentPostProcessor。作为 ApplicationListener,它监听了事件 ApplicationEnvironmentPreparedEvent 和 ApplicationPreparedEvent。ApplicationEnvironmentPreparedEvent 事件发生时,它将 Spring Boot 内置配置的其他 EnvironmentPostProcessor 和自己放到一起,排序,然后应用到应用上下文环境对象上。该 EnvironmentPostProcessor 对应用上下文环境所做的操作就是读取配置文件将它们添加到应用上下文环境中去。ConfigFileApplicationListener 类所在的包路径如下图所示。

ConfigFileApplicationListener 类部分源码如下所示。

复制代码
// Note the order is from least to most specific (last one wins)
	private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";

缺省情况下,配置属性会从以下路径的 application.properties/yml 文件中读取:

  • classpath:/
  • classpath:/config/
  • file:./
  • file:./config/

如注释所言,上述配置文件优先级由低到高,重复的配置被高优先级覆盖,不重复的配置互补。如下图所示,application.properties 优先级从 1 到 4 递减。

三、外部配置

Spring Boot 也可以采用外部配置文件来配置不同环境的属性,具体可以参考官方文档:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config。截取部分文档内容如下,完整文档可以阅读官网。

文章参考:

相关推荐
梁云亮3 小时前
spring整合Junit4
spring
小小猪的春天4 小时前
Java 手写第一个 MCP Server:Spring AI MCP 半小时跑通
java·人工智能·spring boot·ai编程
爱学堂IT分享4 小时前
鹤翔老师pmp项目管理培训认证考试课 (直播录播题库送学时证明)【共58课时】-51CTO学堂
后端
王的宝库5 小时前
Go 项目结构:从单文件到标准工程布局
开发语言·后端·golang
码事漫谈6 小时前
类型即世界:一个 C++ 程序员的本体论入门
后端
Rain的Java大神之路6 小时前
JavaWeb开发如何解决跨域问题
java·前端·后端·nginx·web安全·面试·运维开发
那个松鼠很眼熟w6 小时前
4.Spring-Ai入门案例
java·人工智能·spring
计算机毕设定制辅导-无忧学长6 小时前
《基于SpringBoot青年公寓出租管理系统的设计与实现》
java·vue.js·spring boot·青年公寓出租管理系统
MetaLite8 小时前
AI编程与工程底座-让AI遵守SpringBoot边界
人工智能·spring boot·ai编程
IT_陈寒8 小时前
Python的多线程居然是个假把式?搞清GIL让我少熬三天夜
前端·人工智能·后端