SpringBoot - Spring Profiles 详解

在这里插入图片描述


Pre

SpringBoot - Spring Boot 中的配置体系Profile全面解读

SpringBoot - spring.profiles.active最佳实践


官方文档

https://docs.spring.io/spring-boot/reference/features/profiles.html

Spring Profiles 详解

Spring Profiles 提供了一种将应用程序配置的部分内容隔离并仅在特定环境中可用的方式。通过使用 @Profile 注解,可以限制 @Component@Configuration@ConfigurationProperties 类在特定环境下加载。

接下来我们将详细介绍 Spring Profiles 的使用方法,包括如何激活 Profiles、添加 Active Profiles、Profile Groups 以及如何在代码中设置 Profiles。


1. 基本用法

任何 @Component@Configuration@ConfigurationProperties 类都可以通过 @Profile 注解来限制其加载时机。例如,以下配置类仅在 production Profile 激活时加载:

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration(proxyBeanMethods = false)
@Profile("production")
public class ProductionConfiguration {
    // ...
}

如果 @ConfigurationProperties 类是通过 @EnableConfigurationProperties 注册的,而不是通过自动扫描注册的,则需要在带有 @EnableConfigurationProperties 注解的 @Configuration 类上指定 @Profile 注解。如果 @ConfigurationProperties 类是扫描注册的,则可以直接在 @ConfigurationProperties 类上指定 @Profile 注解。


2. 激活 Profiles

可以通过 spring.profiles.active 环境属性来指定哪些 Profiles 是激活的。可以在 application.properties 中指定该属性,例如:

spring.profiles.active=dev,hsqldb

也可以在命令行中使用 --spring.profiles.active 参数来指定:

java -jar myapp.jar --spring.profiles.active=dev,hsqldb

如果没有激活任何 Profile,则会启用默认的 Profile。默认 Profile 的名称是 default,可以通过 spring.profiles.default 属性进行调整:

spring.profiles.default=none

需要注意的是,spring.profiles.activespring.profiles.default 只能在非 Profile 特定的文档中使用。这意味着它们不能包含在 Profile 特定的文件或由 spring.config.activate.on-profile 激活的文档中。

例如,以下配置是无效的:

spring.profiles.active=prod
#---
spring.config.activate.on-profile=prod
spring.profiles.active=metrics

3. 添加 Active Profiles

spring.profiles.active 属性遵循与其他属性相同的优先级规则:最高优先级的 PropertySource 会生效。这意味着可以在 application.properties 中指定激活的 Profiles,然后通过命令行参数替换它们。

有时,我们希望添加激活的 Profiles 而不是替换它们。可以使用 spring.profiles.include 属性在 spring.profiles.active 激活的 Profiles 基础上添加额外的 Profiles。SpringApplication 入口点还提供了一个 Java API 来设置额外的 Profiles,即 setAdditionalProfiles() 方法。

例如,当运行以下配置的应用程序时,即使使用 --spring.profiles.active 参数,commonlocal Profiles 也会被激活:

spring.profiles.include[0]=common
spring.profiles.include[1]=local

spring.profiles.active 类似,spring.profiles.include 只能在非 Profile 特定的文档中使用。


4. Profile Groups

有时,应用程序中定义和使用的 Profiles 过于细粒度,使用起来会变得繁琐。例如,可能有两个 Profiles:proddbprodmq,分别用于启用数据库和消息功能。

为了解决这个问题,Spring Boot 允许你定义 Profile Groups。Profile Group 允许你为相关的 Profiles 定义一个逻辑名称。

例如,我们可以创建一个 production 组,包含 proddbprodmq Profiles:

spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq

现在,可以通过 --spring.profiles.active=production 来启动应用程序,从而一次性激活 productionproddbprodmq Profiles。

spring.profiles.activespring.profiles.include 类似,spring.profiles.group 只能在非 Profile 特定的文档中使用。


5. 在代码中设置 Profiles

你可以在应用程序运行之前通过调用 SpringApplication.setAdditionalProfiles(...) 来以编程方式设置激活的 Profiles。还可以使用 Spring 的 ConfigurableEnvironment 接口来激活 Profiles。

例如:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        app.setAdditionalProfiles("dev", "hsqldb");
        app.run(args);
    }
}

6. Profile 特定的配置文件

Spring Boot 支持 Profile 特定的配置文件,例如 application-dev.propertiesapplication-prod.yaml。这些文件会根据激活的 Profiles 自动加载。例如,如果激活了 dev Profile,则 application-dev.properties 文件会被加载。

还可以在 @ConfigurationProperties 类中引用 Profile 特定的配置文件。例如:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
    private String name;
    private String description;

    // Getters and setters
}

然后在 application-dev.properties 中指定属性:

myapp.name=MyApp (Dev)
myapp.description=This is the development environment.

总结

Spring Profiles 提供了一种灵活的方式来管理不同环境下的应用程序配置。通过 @Profile 注解,可以轻松地控制配置类和组件的加载时机。通过 spring.profiles.activespring.profiles.include 属性,可以灵活地激活和组合 Profiles。Profile Groups 则进一步简化了多个相关 Profiles 的管理。此外,Spring Boot 还支持 Profile 特定的配置文件,使得不同环境下的配置管理更加方便。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值