侧边栏壁纸
  • 累计撰写 49 篇文章
  • 累计创建 23 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Spring Boot 多环境配置指南

阿砖
2025-03-05 / 0 评论 / 0 点赞 / 90 阅读 / 7545 字

本文档详细介绍了如何在 Spring Boot 项目中配置多环境支持(开发、生产、本地等),包括 Maven 的 pom.xml​ 配置、Spring Boot 的 application.yml​ 文件调整,以及相应的测试代码验证。


1. 配置 Maven 的 pom.xml

1.1 定义 Profiles

pom.xml​ 中添加 <profiles>​ 标签,用于支持不同的运行环境(如 dev​、prod​、local​)。每个 <profile>​ 定义了对应的属性值,并通过 <activation>​ 设置默认激活环境。

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profile.active>dev</profile.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <profile.active>prod</profile.active>
        </properties>
    </profile>
    <profile>
        <id>local</id>
        <properties>
            <profile.active>local</profile.active>
        </properties>
    </profile>
</profiles>
  • <id>​:Profile 的唯一标识。

  • <properties>​:定义动态属性 profile.active​,用于后续配置文件中引用。

  • <activation>​:设置 dev​ 环境为默认激活状态。

1.2 配置资源过滤

<build>​ 标签中启用资源过滤,确保 Maven 在构建时将 profile.active​ 的值注入到配置文件中。

<build>
<resources>
          <resource>
              <directory>src/main/resources</directory>
              <!-- 关闭过滤 -->
              <filtering>false</filtering>
          </resource>
          <resource>
              <directory>src/main/resources</directory>
              <!-- 引入所有 匹配文件进行过滤 -->
              <includes>
                  <include>application*</include>
                  <include>bootstrap*</include>
                  <include>banner*</include>
              </includes>
              <!-- 启用过滤 即该资源中的变量将会被过滤器中的值替换 -->
              <filtering>true</filtering>
          </resource>
        </resources>
</build>
  • <filtering>true</filtering>​:启用资源过滤,允许 Maven 替换占位符。


2. 配置 Spring Boot 的 application.yml

2.1 主配置文件

src/main/resources/application.yml​ 中配置 Spring 的活跃 Profile,使用占位符 @profile.active@​ 与 Maven 的 pom.xml​ 中的属性值动态绑定。

spring:
  profiles:
    active: @profile.active@  # 动态引用 Maven 定义的 profile.active 属性

2.2 环境特定配置文件

src/main/resources​ 目录下创建以下环境特定的配置文件(内容可为空,后续根据需要填充):

  • application-dev.yml​:开发环境配置文件。

  • application-local.yml​:本地环境配置文件。

  • application-prod.yml​:生产环境配置文件。

这些文件将根据 spring.profiles.active​ 的值被加载。


3. 创建测试类验证配置

为了验证多环境配置是否生效,可以创建以下测试类,利用 Spring 的 @Profile​ 注解和 @PostConstruct​ 方法在特定环境下输出初始化信息。

3.1 开发环境配置类

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

/**
 * 开发环境配置类,仅在 "dev" Profile 下生效。
 */
@Configuration
@Profile("dev")
public class DevConfig {
    @PostConstruct
    public void init() {
        System.out.println("DevConfig initialized");
    }
}

3.2 本地环境配置类

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

/**
 * 本地环境配置类,仅在 "local" Profile 下生效。
 */
@Configuration
@Profile("local")
public class LocalConfig {
    @PostConstruct
    public void init() {
        System.out.println("LocalConfig initialized");
    }
}

3.3 生产环境配置类

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

/**
 * 生产环境配置类,仅在 "prod" Profile 下生效。
 */
@Configuration
@Profile("prod")
public class ProdConfig {
    @PostConstruct
    public void init() {
        System.out.println("ProdConfig initialized");
    }
}
  • @Profile​:指定类仅在对应 Profile 下加载。

  • @PostConstruct​:在类初始化后执行,输出验证信息。


4. 构建与运行项目

4.1 构建命令

使用 Maven 构建项目,并通过 -P​ 参数指定激活的 Profile。例如,激活 dev​ 环境:

mvn -DskipTests=true clean install -P dev,!prod,!local
  • -DskipTests=true​:跳过测试以加快构建。

  • -P dev,!prod,!local​:激活 dev​ Profile,禁用 prod​ 和 local​。

提示:若使用 IntelliJ IDEA,可在右侧 Maven 菜单中勾选对应 Profile,无需手动输入命令。

4.2 运行项目

构建完成后,使用以下命令运行生成的 JAR 文件:

java -jar target/your-application.jar

运行 dev​ 环境时,控制台应输出类似以下内容:

2025-03-05 15:59:41.903  INFO 30516 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 596 ms
DevConfig initialized
2025-03-05 15:59:42.044  INFO 30516 --- [main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
  • 输出 DevConfig initialized​ 表示 dev​ Profile 配置成功加载。

通过上述配置,项目实现了基于 Maven 和 Spring Boot 的多环境支持。开发者可以根据需要切换 dev​、prod​ 或 local​ 环境,并在特定环境下加载对应的配置和逻辑。这种方法适用于开发、测试和生产环境的灵活管理。

0

评论区