You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot识别正确Profile名称却读取默认Profile属性值求助

问题分析与解决方案

你的问题主要出在命令行参数拼写错误和YAML配置文件的Profile定义格式错误两个地方,以下是具体分析和修正方案:

1. 命令行参数拼写错误

你当前使用的启动命令里,Profile激活参数的名称写错了:

java -jar client-stp-grid-publisher-SNAPSHOT.jar --spring.profile.active=qa

Spring Boot中正确的参数名是复数形式的spring.profiles.active,少了一个s会导致框架无法正确识别你要激活的Profile,最终自动 fallback 到默认Profile。修正后的命令:

java -jar client-stp-grid-publisher-SNAPSHOT.jar --spring.profiles.active=qa

2. YAML配置文件的Profile定义错误

你的application.yml中,第二个文档的Profile定义存在语法问题。在Spring Boot的多Profile YAML配置中,每个独立文档需要用spring.profiles指定该文档对应的Profile名称;而spring.profiles.default是用来设置默认激活的Profile(当未指定spring.profiles.active时生效)。

你当前的错误写法:

spring: profiles: default: dev

这种扁平化的结构不符合YAML的层级语法,正确的写法需要拆分层级,明确第二个文档对应的Profile为dev,同时可按需设置默认激活规则。

修正后的完整application.yml配置:

spring:
  profiles: qa
  datasource:
    url: "jdbc:oracle:thin:@qfxo001z.unix:1523/AXLET02Q"
    username: "axle"
    #password will be loaded from secured external file location
    driver-class-name: "oracle.jdbc.driver.OracleDriver"
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
---
banner:
  location: "config/banner.txt"
logging:
  config: "classpath:config/log4j2-spring.xml"
spring:
  profiles: dev
  profiles:
    default: dev # 可选:设置未指定active时默认激活dev
  datasource:
    url: "jdbc:oracle:thin:@localhost:1521/XE"
    username: "axle"
    #password will be loaded from secured external file location
    driver-class-name: "oracle.jdbc.driver.OracleDriver"
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
      properties:
        hibernate:
          dialect: org.hibernate.dialect.OracleDialect
db:
  password:
    propertyFilePath: "/tmp/db-password.properties"
instances: &instances
  - DAPI_0
  - DAPI_1
  - DAPI_2
  - DAPI_R1
  - CNX
chronicle:
  inputQueuePath: /tmp/${INSTANCE}/queue-v1/input
  outputQueuePath: /tmp/${INSTANCE}/queue-v1/output

额外注意事项

  • 确保YAML配置使用空格缩进(不要用Tab键),缩进错误是YAML配置无法解析的常见原因;
  • 如果${INSTANCE}变量需要外部传入,记得在启动命令中补充参数,比如--INSTANCE=DAPI_0,否则会导致路径解析失败。

内容的提问来源于stack exchange,提问作者Shashi Shankar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:22:55