首页技术栈归档照片墙音乐日记随想收藏夹友链留言关于

多 Profile 文件方式

写作时间:2026-07-08

多 Profile 文件方式

properties 配置

在 helloworld 的 src/main/resources 下添加 4 个配置文件:

  • application.properties:主配置文件
  • application-dev.properties:开发环境配置文件
  • application-test.properties:测试环境配置文件
  • application-prod.properties:生产环境配置文件

在 applcation.properties 文件中,指定默认服务器端口号为 8080,并通过以下配置激活生产环境(prod)的 profile。

#默认端口号server.port=8080
#激活指定的spring.profiles.active=prod

在 application-dev.properties 中,指定开发环境端口号为 8081,配置如下

# 开发环境server.port=8081

在 application-test.properties 中,指定测试环境端口号为 8082,配置如下。

# 测试环境server.port=8082

在 application-prod.properties 中,指定生产环境端口号为 8083,配置如下。

# 生产环境server.port=8083

yml 配置

与 properties 文件类似,我们也可以添加 4 个配置文件

  • application.yaml:默认配置
  • application-dev.yaml:开发环境配置
  • application-test.yaml:测试环境配置
  • application-prod.yaml:生产环境配置

在 applcation.yml 文件中指定默认服务端口号为 8080,并通过以下配置来激活开发环境的 profile。

#默认配置
server:
  port: 8080
#切换配置
spring:
  profiles:
    active: dev #激活开发环境配置

在 application-dev.yml 中指定开发环境端口号为 8081,配置如下。

#开发环境
server:
  port: 8081

在 application-test.yml 中指定测试环境端口号为 8082,配置如下。

#测试环境
server:
  port: 8082

在 application-prod.yml 中指定生产环境端口号为 8083,配置如下。

#生产环境
server:
  port: 8083

多 Profile 文档块模式

application.yaml

#默认配置
server:
  port: 8080
#切换配置
spring:
  profiles:
    active: test
---
#开发环境
server:
  port: 8081
spring:
  config:
    activate:
      on-profile: dev
---
#测试环境
server:
  port: 8082
spring:
  config:
    activate:
      on-profile: test
---
#生产环境
server:
  port: 8083
spring:
  config:
    activate:
      on-profile: prod
#关闭图标 只需把图标放在配置目录下即可 将favicon.icon放到resources目录下  例如:/public,/static等等,现在以淘汰
spring.mvc.favicon.enabled=false
avatar

yuanyourdomain

写代码,做研究,记录生活。

RECOMMENDED

MyBatis 动态 SQL

2026-07-08

Nginx 基础入门

2026-07-08

Maven 多模块与私服

2026-07-08