对于 Springboot 考虑配置 yaml 文件而不是属性文件,有什么特别要说的吗?我在项目中使用 Maven 和 Springboot,如果我将文件 application.properties 放在资源文件中,Springboot 会自动识别配置。
然而,仅仅用 yml 文件替换属性文件是行不通的,该文件不再被考虑在内。
有什么要添加到 splicitely 告诉 SpringBoot 使用 yml 文件吗?
在两个文件下面:
应用程序属性:
# 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
最佳答案
如果您引用 Spring Boot 引用文档中的“使用 YAML 而不是属性”部分,它会说 Spring Boot 应该自动选择它,因为您的类路径上有 SnakeYAML 库。
话虽如此,它也说明了
If you use ‘starter POMs’ SnakeYAML will be automatically provided via spring-boot-starter
关于java - 如何在 Spring Boot 中用 yml 文件替换属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33350629/