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?
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With