一文掌握 Spring Boot Profiles

Spring Boot Profiles 简介

Profile 的概念其实很早在 Spring Framework 就有了,在 Spring Framework 3.1 版本引入了注解 @ProfileEnvironment 环境配置的抽象,只是在 Spring Boot 框架里再进一步将 Profiles 功能进行扩展,使它也成为了 Spring Boot 特性之一,为此单独在 官方文档 25. Profiles 一节里介绍,文档里把 Spring Boot Profiles 也叫作 Spring Profiles。html

那么什么又是 Spring Profiles,为何须要它呢?咱们先来看一个熟悉的场景:咱们日常项目开发,常常须要根据不一样的环境进行配置的修改,好比在本地开发会加载本机的配置和开发环境数据库,在测试服务器上部署时就须要加载测试环境配置和数据库,一样地,当项目发布生产环境时就须要设置为生产环境配置和数据库。这样一来,不一样的环境部署都须要额外的处理来调整环境的配置,维护起来十分繁琐,还容易出错。git

为了解决不一样环境须要的配置切换问题,Spring Profiles 提供了一种方式容许咱们指定在特定环境下只加载对应的程序配置,每一种环境配置对应一个 Profile,只有当前 Profile 处于激活状态时,才会将该 Profile 所对应的配置和 Bean 加载到 Spring 程序中。github

Spring Profiles 就是针对应用程序,不一样环境须要不一样配置加载的一种解决方案。spring

固然 Spring 容许多个 Profile 处于激活状态,好比将应用配置进行细分红数据库配置,消息中间件配置,缓存配置等,都为各自在不一样环境定义不一样的 Profile 名称,在须要激活环境对应配置时,指定多个 Profile。数据库

Spring Profiles 实战

在 Spring 程序中有两种方式使用 Profiles:XML 配置和注解 @Profile设计模式

XML 配置定义 Profile

虽然如今 XML 配置方式使用愈来愈少,仍是简单介绍下,一般咱们在 XML 文件定义的 Bean 时都有根元素 <beans>,在 beans 元素上多了一个属性 profile 能够指定环境,好比说把开发环境的 profile 定义为 dev,生产环境的 profile 为:prod。缓存

须要注意的是:必需要使用 Spring XML Beans Schema 版本为 4.0 以上才支持 profile 属性。在 XML 文件定义以后咱们只须要激活指定的 Profile 名称就能够加载对应的 Bean 对象了,在 Spring 程序中激活的方式主要两种:springboot

  • Java API 方式,获取当前 Spring 容器的环境 Bean,设置 activeProfiles 属性,而后启动容器服务器

  • 采用启动参数方式指定,固定格式:-Dspring.profiles.active=dev

注解 @Profiles 定义Profile

使用注解定义 Profile 也比较简单,引入一个新的注解 @Profiles,一般 @Profiles 配合 @Component 或者 @Configuration 使用,以下示例:app

激活 Profile 的方式都是同样的,只要指定 Profile 被激活,其对应的 Bean 才会加载。在 Spring 程序中 Profile 默认为 default,当前咱们能够经过 spring.profiles.default 配置方式或者 org.springframework.core.env.AbstractEnvironment#setDefaultProfiles API 方式修改。

Spring Boot Profile 实战

好了,如今咱们再来看下在 Spring Boot 程序中如何使用 Profile。一般一个 Spring Boot 程序的配置文件为 yml 或者 properties 格式,因为 yml 格式文件的结构简洁已读,备受官方推崇,咱们能够看下如何在 application.yml 定义 Profile 和对应的配置。

与yml格式文件不一样,正对不一样的 Profile,没法在一个 properties 文件设置,官方采用命名形式为 applications-${profile}.properties 格式来达成同样的效果。为了看到指定 Profile 激活后的效果,咱们能够经过下方的一个例子实践下,经过激活不一样 Profile 启动程序,来请求 /enviroment 接口来获取当前的环境配置变量。

这里咱们介绍如何在配置文件中激活 Profile 的方式:在 application.yml 顶部添加以下配置,代表当前所激活的 Profile 为 prod,固然也能够前文介绍的启动参数方式激活:

而后启动程序,curl 方式访问 http://localhost:9000/enviroment 能够获得以下输出结果:

一样若是上述的 active 属性值指定为 dev,将输出内容: current app enviroment is prod

Spring Boot API 方式激活 Profile

在 Spring Boot 程序除了上述的方法来激活 Profile 外,还可使用 Spring Boot API 方式激活:

  • SpringApplication.setAdditionalProfiles(…)

  • SpringApplicationBuilder.profiles(...)

但须要注意的是使用 Spring Boot API 的话须要在程序启动前设置,也就是 SpringApplication.run(...) 方法执行前,不然没有效果。 采用 Spring Boot API 方式添加的Profile 是属于额外激活的 Profile,也就是说覆盖掉外部传入的 spring.profiles.activie 指定的 Profile。

总结

在Spring Boot 程序中,咱们一般定义不一样 Profiles 的配置文件,如 application-{profile}.properties,在默认配置文件 application.properties 中设置 spring.profiles.active=dev ,用于日常开发使用,当须要打包上传服务器时,经过启动参数方式 jar -Dspring.profiles.active=prod xxx.jar 指定对应环境的 Profile 启动程序来加载对应环境的配置,到这里咱们学习如何经过 Spring Boot Profiles 特性来应对程序中不一样环境配置的切换,但愿对工做中的小伙伴有所帮助,也欢迎小伙伴留言分享应对项目环境配置区分加载的实践心得。如有错误或者不当之处,还请你们批评指正,一块儿学习交流。

下篇文章将经过解读源码的方式具体讲解 Spring Boot Profiles 实现原理,敬请关注期待。

示例代码

本文示例代码能够经过下面仓库地址获取:

环境支持:

  • JDK 8
  • SpringBoot 2.1.6
  • Maven 3.6.0

参考资料

推荐阅读

相关文章
相关标签/搜索