Spring Profile包含yaml文件的问题

9
我正在尝试实现的目标是在团队设置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时,我收到以下错误:

由于在“reader”中不允许映射值,因此引发以下错误:第28行,第12列:           include: cloud


请注意,这只是简单的翻译,并不能保证在具体场景下一定正确,建议结合上下文理解。

尝试去掉 include 前面的 - Marged
@Marged,如何使include不嵌套在profiles中? - ndrone
我最初看了那篇文章,但它没有给我想要的结果。我只想在websphere处于活动状态时使用cloud。如果将websphere配置文件更改为--- spring: profiles: active:websphere include:cloud test:websphere。它总是加载cloud配置文件。我正在使用Spring 4.2和Spring Boot 1.3.1。 - ndrone
尝试升级到1.3.2并检查经典的application.properties是否能够胜任。 - Marged
让我们在聊天中继续这个讨论 - ndrone
显示剩余3条评论
2个回答

8
这似乎是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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接