从 Applications.properties 启动 Spring 的数据库 application.yml [英] Database application.yml for Spring boot from applications.properties

查看:34
本文介绍了从 Applications.properties 启动 Spring 的数据库 application.yml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到 Postgres 数据库的有效 Spring Boot 应用程序.我已经使用 application.properties 文件设置了项目,但希望切换到 application.yml 文件.但是,当我进行切换时,我的应用程序在尝试连接到数据库时出错.

I've got a working Spring Boot Application that connects to a Postgres database. I've got the project set up with an application.properties file, but would like to make the switch over to an application.yml file. However when I make the switch, my application errors out while attempting to connect to the db.

原始 applications.properties 文件:

Original applications.properties file:

spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=foo
spring.datasource.password=bar

这是我目前在 application.yml 文件中的内容:

And Here's what I've got so far in the application.yml file:

spring.jpa:
  database: POSTGRESQL
  hibernate.ddl-auto: create-drop
  show-sql: true

spring.datasource:
  platform: postgres
  driverClassName: org.postgresql.Driver
  url: jdbc:postgresql://localhost:5432/mydb
  username: foo
  password: bar

我是否在文件类型之间的转换中遗漏了什么?

Am I missing something in the translation between file types?

推荐答案

您需要将属性名称中的每个 . 字符视为 yaml 文件中的级别:

You need to treat each . character in property names as levels in the yaml file:

spring:
  jpa:
    database: POSTGRESQL
    show-sql: true
    hibernate:
      ddl-auto: create-drop
  datasource:
    platform: postgres
    url: jdbc:postgresql://localhost:5432/mydb
    username: foo
    password: bar
    driverClassName: org.postgresql.Driver

EDIT:已建议修改,谢谢.driverClassName 属性实际上应该在 spring.datasource 下.然而,这个答案的目的是展示如何将 properties 文件转换为 yaml 格式.所以我已经将 driverClassName 属性更改为正确的路径,这不是从 propertiesyaml 转换的一部分.

EDIT: edits have been suggested, thanks for that. The driverClassName property actually should be under spring.datasource. However, the purpose of this answer was to show how a properties file is converted into yaml format. So I have changed the driverClassName property to be at the right path, that is not part of the transformation from properties to yaml.

这篇关于从 Applications.properties 启动 Spring 的数据库 application.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆