Spring Profile包含yaml文件的问题

问题描述 投票:6回答:2

当团队设置websphere配置文件激活时,我正在尝试完成云配置文件也已激活。

yaml文件

   ---
    spring:
      application:
        name: kicapp
      output:
        ansi:
          enabled: ALWAYS
      profiles:
        active: local
    #server:
      #context-path: /
      #port: 8080
    #logging:
      #level:
        #org.springframework.security: DEBUG
    ---
    spring:
      profiles: local
    ---
    spring:
      profiles: unittest
    ---
    spring:
      profiles: cloud
      test: loaded
    ---
    spring:
      profiles: websphere
        include: cloud

当我设置--spring.profiles.active=websphere时,我收到以下错误

引起:'读者',第28行,第12栏中不允许使用映射值:include:cloud

java spring spring-boot snakeyaml
2个回答
6
投票

这似乎是对SnakeYAML解析器的限制以及Spring Boot使用它的方式。由于yaml允许使用---分隔符在单个文件中指定多个不同的文档,因此Spring分离出单独文档的方式是使用spring.profiles键,这个键很可能是一个简单的结构而不是复杂的结构。

一个好的解决方法实际上是通过这种方式将其拆分为多个文件:

application.yaml具有共同内容,application-<profile>具有特定于配置文件的扩展,具有此结构的关键spring.profiles.include将按预期工作。


2
投票

根据Biju的回答,如果您想使用---分隔符保留单个文件而不使用单独的配置文件,则可以按如下方式定义spring.profiles.include密钥:

---
spring:
  profiles: currentProfile
  profiles.include: profileToInclude

这样,解析并不存在复杂的结构。

© www.soinside.com 2019 - 2024. All rights reserved.