Spring Boot Profiles
最后修改于 2023 年 7 月 29 日
在本文中,我们将展示如何在 Spring Boot 应用程序中使用 profiles。
Spring Boot 是一个流行的应用程序框架,用于在 Java、Kotlin 或 Groovy 中创建企业应用程序。
Spring Boot profiles
应用程序的开发过程有不同的阶段;典型的阶段是开发、测试和生产。Spring Boot profiles 将应用程序配置的各个部分分组,并使其仅在某些环境中可用。
一个 profile 是一组配置设置。Spring Boot 允许以 application-{profile}.properties 的形式定义特定于 profile 的属性文件。它会自动加载所有 profiles 的 application.properties 文件中的属性,以及特定于 profile 的属性文件中的属性,仅适用于指定的 profile。特定于 profile 的属性中的键会覆盖主属性文件中的键。
@Profile 注解指示当指定的 profile 或 profiles 处于活动状态时,组件才有资格注册。默认 profile 称为 default;所有未设置 profile 的 bean 都属于此 profile。
Spring Boot 中定义活动 profiles 的方法有很多,包括命令行参数、Maven/Gradle 设置、JVM 系统参数、环境变量、spring.profiles.active 属性和 SpringApplication 方法。
在集成测试中,profiles 使用 @ActiveProfiles 激活。
Spring Boot profiles 示例
在以下应用程序中,我们有三个 profiles (local, dev, prod) 和两个特定于 profile 的属性文件。我们使用 spring.profiles.active 来设置活动 profiles,并使用 SpringApplicationBuilder 的 profiles 方法来添加新的活动 profiles。
build.gradle
...
src
├── main
│ ├── java
│ │ └── com
│ │ └── zetcode
│ │ └── Application.java
│ └── resources
│ ├── application-dev.properties
│ ├── application-prod.properties
│ └── application.properties
└── test
└── java
这是项目结构。
plugins {
id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
}
group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
这是 build.gradle 文件。
spring.profiles.active=local
我们在 application.properties 文件中设置一个 profile。
message=Dev message
在 application-dev.properties 中,我们有一个 message 属性。
message=Prod message
在 application-prod.properties 中,我们定义了相同的属性。
package com.zetcode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.profiles("dev", "prod")
.run(args);
}
}
@Component
class MyRunner implements CommandLineRunner {
@Autowired
private Environment environment;
@Override
public void run(String... args) throws Exception {
System.out.println("Active profiles: " +
Arrays.toString(environment.getActiveProfiles()));
}
}
@Component
@Profile(value="dev")
class MyRunner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("In development");
}
}
@Component
@Profile(value="prod & !dev")
class MyRunner3 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("In production");
}
}
@Component
@Profile(value="local")
class MyRunner4 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("In local");
}
}
@Component
@Profile(value={"dev & local"})
class MyRunner5 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("In development and local");
}
}
@Component
@Profile(value={"dev", "prod"})
class MyRunner6 implements CommandLineRunner {
@Value("${message}")
private String message;
@Override
public void run(String... args) throws Exception {
System.out.println("Message: " + message);
}
}
在 application.properties 中,我们已将 local profile 设置为活动状态。通过 SpringApplicationBuilder 的 profiles 方法,我们添加了两个额外的 profiles。使用 @Profile 注解,我们确定哪些 runner beans 被注册。
@Component
class MyRunner implements CommandLineRunner {
@Autowired
private Environment environment;
@Override
public void run(String... args) throws Exception {
System.out.println("Active profiles: " +
Arrays.toString(environment.getActiveProfiles()));
}
}
此 bean 始终运行;它输出所有活动 profiles。
@Component
@Profile(value="dev")
class MyRunner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("In development");
}
}
当 dev profile 处于活动状态时,此 runner 执行。
@Component
@Profile(value="prod & !dev")
class MyRunner3 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("In production");
}
}
当 prod profile 处于活动状态且 dev 不活动时,此 bean 执行。
@Component
@Profile(value={"dev & local"})
class MyRunner5 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("In development and local");
}
}
当 dev 和 local profiles 都处于活动状态时,此 bean 执行。
@Component
@Profile(value={"dev", "prod"})
class MyRunner6 implements CommandLineRunner {
@Value("${message}")
private String message;
@Override
public void run(String... args) throws Exception {
System.out.println("Message: " + message);
}
}
此 bean 为 dev 或 prod profiles(或两者)执行。输出什么消息取决于最后加载的 profile。
$ ./gradlew bootRun ... Active profiles: [dev, prod, local] In development In local In development and local Message: Prod message
通过在 application.properties 文件中设置 local profile,并使用 SpringApplicationBuilder 的 profiles 方法添加 dev 和 prod profiles,我们得到此输出。我们得到 "Prod message",因为 prod profile 在 dev 之后指定。
在本文中,我们使用了 Spring Boot profiles。