Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database application.yml for Spring boot from applications.properties

Tags:

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.

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 

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?

like image 503
user3051261 Avatar asked Oct 24 '15 22:10

user3051261


People also ask

Can we use application properties and application yml together?

Overview. A common practice in Spring Boot is using an external configuration to define our properties. This allows us to use the same application code in different environments. We can use properties files, YAML files, environment variables and command-line arguments.

Is application yml same as application properties?

One notable difference is how the properties are represented in each file. YAML files may use consistent spaces to denote hierarchy whereas properties file may use = to denote property values. For ex. Another difference is we can add multiple configuration files into single yaml file.

Where do you put database properties in Spring Boot application?

To access the Relational Database by using JdbcTemplate in Spring Boot application, we need to add the Spring Boot Starter JDBC dependency in our build configuration file. Then, if you @Autowired the JdbcTemplate class, Spring Boot automatically connects the Database and sets the Datasource for the JdbcTemplate object.

How do I put properties in Spring Boot application properties?

Properties files are used to keep 'N' number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application. properties file under the classpath. Note that in the code shown above the Spring Boot application demoservice starts on the port 9090.


1 Answers

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: 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.

like image 56
Zoltan Avatar answered Sep 18 '22 12:09

Zoltan