java - 如何在 Spring Boot 中用 yml 文件替换属性文件

标签 java spring spring-boot

对于 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/

相关文章:

spring-boot - 在 spring boot 和 OAuth2 应用程序中禁用同一用户的多次登录

spring-boot - Spring Batch 分页阅读器和异常处理

java - 托管在 Heroku 上的 Spring Boot 项目 - Web 进程在启动后 90 秒内无法绑定(bind)到 $PORT

java - 在由许多子类扩展的父类(super class)中 Autowiring Spring bean

java - 从数组中提取信息

spring - 对象引用未保存的 transient 实例 - Spring、JPA Hibernate

java - Spring如何在Spring Boot中设置数据源

java - 在 Spring petclinic 中以 jsp jSTL 形式创建 boolean 字段

java - Gson序列化问题与反斜杠java

java - Micronaut 服务未响应请求