【问题标题】:How to replace properties files by yml files in Spring boot如何在 Spring Boot 中用 yml 文件替换属性文件
【发布时间】:2015-10-26 16:18:51
【问题描述】:

Springboot 有什么特别要说的,要考虑配置 yaml 文件而不是属性文件吗?我在项目中使用 Maven 和 Springboot,如果我将文件 application.properties 放在资源文件中,Springboot 会自动识别配置。

但是,仅用 yml 文件替换属性文件是行不通的,该文件不再考虑在内。

有什么要添加到 SpringBoot 以使用 yml 文件吗?

两个文件下方:

application.properties:

# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=/WEB-INF/views/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

和application.yml

spring:
  profiles:
    active: ${dario.environment:dev}
  thymeleaf:
    check-template-location: true
    thymeleaf.prefix: /WEB-INF/views/
    thymeleaf.suffix: .html
    thymeleaf.mode: LEGACYHTML5
    spring.thymeleaf.encoding: UTF-8
    spring.thymeleaf.content-type: text/html
    cache: false

【问题讨论】:

  • 嘿,你是怎么在你的 yml 文件中写 active: ${dario.environment:dev} 的?你的 yml 文件是从哪里读取的?
  • 一个系统属性,即来自命令行-Ddario.environment=prod,如果我没有指定属性,默认会采用dev。希望这会有所帮助

标签: java spring spring-boot


【解决方案1】:

如果您参考 Spring Boot 参考文档中的“使用 YAML 而不是属性”部分,它表示 Spring Boot 应该自动选择它,因为您的类路径中有 SnakeYAML 库。

话虽如此,它也说明了

如果您使用“starter POM”,SnakeYAML 将通过以下方式自动提供 弹簧启动器

【讨论】:

  • 谢谢,我的类路径上有 SnakeYAML,我刚刚发现了问题。我的 YML 文件格式不正确。我在重复百里香叶这个词。固定:)
猜你喜欢
  • 2021-05-23
  • 2019-06-04
  • 1970-01-01
  • 2020-02-14
  • 2017-09-25
  • 1970-01-01
  • 2016-07-17
  • 2017-09-07
  • 2014-10-03
相关资源
最近更新 更多