This tutorial will teach you how to bind nested properties from the application.properties file to a Java class.
Once you finish this tutorial, you might also want to check:
- How to bind simple(not nested) properties to a Java class, and
- How to use @ConfigurationProperties with Spring Boot @Profile.
Example of Nested Properties in a Properties File
Your application.properties file might have nested properties like the ones below:
database.name = photo_app database.url = jdbc:mysql://localhost:3306/${database.name} database.userCredentials.username = developer database.userCredentials.password = 12345678
In this tutorial, you will learn how to read all of the values from the above-mentioned property file, including values of deeply nested properties like:
- database.userCredentials.username and,
- database.userCredentials.password
Using @ConfigurationProperties
To bind nested properties from the above-mentioned application.properties file, we will need to create two Java classes:
- UserCredentials – A Java class to bind properties username and password,
- DatabaseConfiguration – A Java class to bind properties database.name, database.url and database.userCredentials.
UserCredentials Java Class
package com.appsdeveloperblog.tutorials.profiles.profilestutorial.shared; public class UserCredentials { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
DatabaseConfiguration Java Class
package com.appsdeveloperblog.tutorials.profiles.profilestutorial.shared; @ConfigurationProperties("database") publi class DatabaseConfiguration { private String name; private String url; private UserCredentials userCredentials; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public UserCredentials getUserCredentials() { return userCredentials; } public void setUserCredentials(UserCredentials userCredentials) { this.userCredentials = userCredentials; } }
Please note the use of @ConfigurationProperties(“database”) above the DatabaseConfiguration class name. This annotation and the value it holds will help us bind properties from application.properties file to class fields of DatabaseConfiguration class.
The “database” value, which we have specified in @ConfigurationProperties will be used as a prefix to class fields like “name”, “url” and “userCredentials” thus resulting in database.name, database.url, database.userCredentials being mapped to respective properties in an application.properties file.
I hope this tutorial was helpful to you.
Read the following tutorial to learn how to use @ConfigurationProperties with Spring Boot @Profile.
If you are interested in learning more about Spring Boot, look at the below Spring Boot online video courses.