全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-23 10 分钟 ✍️ juanwangdev

Profile 外部化配置管理

外部配置文件分离环境配置,Profile 动态加载。

外部配置方式

方式对比

方式说明
Profile 属性pom.xml 内定义
外部文件单独配置文件
环境变量系统环境注入

外部配置文件

目录结构

properties
project/
├── pom.xml
├── config/
│   ├── dev.properties
│   ├── test.properties
│   └── prod.properties

配置文件内容

XML
# dev.properties
db.url=jdbc:mysql://localhost:3306/dev
db.user=dev
db.password=dev123
api.url=http://localhost:8080/api

Profile 加载外部文件

使用 properties-maven-plugin

Bash
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>properties-maven-plugin</artifactId>
  <version>1.0.0</version>
  <executions>
    <execution>
      <phase>initialize</phase>
      <goals>
        <goal>read-project-properties</goal>
      </goals>
      <configuration>
        <files>
          <file>${project.basedir}/config/${env}.properties</file>
        </files>
      </configuration>
    </execution>
  </executions>
</plugin>

<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <env>dev</env>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <env>prod</env>
    </properties>
  </profile>
</profiles>

执行

XML
mvn package -Pdev   # 加载 config/dev.properties
mvn package -Pprod  # 加载 config/prod.properties

Spring Boot 外部配置

application-{profile}.properties

properties
src/main/resources/
├── application.properties
├── application-dev.properties
├── application-prod.properties

Maven Profile 控制

XML
<profile>
  <id>dev</id>
  <properties>
    <spring.profiles.active>dev</spring.profiles.active>
  </properties>
</profile>

<profile>
  <id>prod</id>
  <properties>
    <spring.profiles.active>prod</spring.profiles.active>
  </properties>
</profile>

application.properties

Bash
spring.profiles.active=@spring.profiles.active@

环境变量注入

Profile 配置

XML
<profile>
  <id>prod</id>
  <properties>
    <db.url>${env.DB_URL}</db.url>
    <db.user>${env.DB_USER}</db.user>
    <db.password>${env.DB_PASSWORD}</db.password>
  </properties>
</profile>

CI/CD 注入

Bash
export DB_URL=jdbc:mysql://prod-server:3306/prod
export DB_USER=prod_user
export DB_PASSWORD=prod_password
mvn package -Pprod

settings.xml 外部配置

用户级配置

XML
<settings>
  <profiles>
    <profile>
      <id>local-config</id>
      <properties>
        <db.url>jdbc:mysql://localhost:3306/local</db.url>
      </properties>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>local-config</activeProfile>
  </activeProfiles>
</settings>

配置文件排除版本库

.gitignore

Bash
config/
*.properties
credentials.properties

提供模板

Bash
config/
├── dev.properties.template
├── prod.properties.template

开发者复制模板创建实际配置。

加密敏感配置

Maven Settings 加密

YAML
mvn --encrypt-password prod_password

使用加密密码

JSON
<server>
  <id>prod-db</id>
  <password>{encrypted_password}</password>
</server>

配置验证

检查配置加载

text
mvn help:effective-pom -Pdev
mvn help:effective-settings

验证属性值

text
mvn properties:read-project-properties -Pdev

多环境配置管理最佳实践

推荐做法

做法说明
配置分离外部文件存放配置
模板提供提供配置模板
敏感加密加密密码等敏感信息
文档说明配置项说明文档

不推荐做法

做法风险
pom.xml 存密码密码暴露版本库
硬编码配置无法环境切换
明文传输安全风险

配置文件格式

YAML 格式

text
# config-dev.yml
database:
  url: jdbc:mysql://localhost:3306/dev
  user: dev
  password: dev123
api:
  url: http://localhost:8080

JSON 格式

text
{
  "database": {
    "url": "jdbc:mysql://localhost:3306/dev",
    "user": "dev"
  }
}

要点总结

  • 外部配置文件分离环境配置
  • properties-maven-plugin 加载外部文件
  • ${env}.properties 动态选择配置文件
  • Spring Boot 使用 application-{profile}.properties
  • 环境变量注入敏感配置
  • settings.xml 用户级配置
  • 配置文件排除版本库,提供模板
  • 加密敏感配置信息

📝 发现内容有误?点击此处直接编辑

← 上一篇 Profile 基础与激活方式
下一篇 → Profile 条件激活机制
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库