Spring Profile包含yaml文件的问题

ndr*_*one 7 java spring snakeyaml spring-boot

当团队设置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
Run Code Online (Sandbox Code Playgroud)

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

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

Bij*_*men 7

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

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

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


小智 5

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

---
spring:
  profiles: currentProfile
  profiles.include: profileToInclude
Run Code Online (Sandbox Code Playgroud)

这样,它就不会解析复杂的结构,并且两个键都存在。