@ConfigurationProperties is used to bind all the properties in a properties file to a Java class annotated with @ConfigurationProperties. You can then use a Java object to access configuration properties defined in a properties file.
Earlier I have shared with you a tutorial on how to read application properties in Spring Boot RESTful Web Service application. In this tutorial you will learn how to map properties in a specific properties file to a Java object.
Application Properties File
Let’s assume we have the following application.properties file.
database.name = photo_app database.url = jdbc:mysql://localhost:3306/${database.name} database.username = "developer" database.password = "12345678"
Please note that each property starts with a prefix “database”. It can be any other prefix but having a prefix helps to map all the properties that start with “database” to a Java object much easier.
Let’s learn how to map these properties to a Java object.
Using @ConfigurationProperties
To map the above properties to a Java object we will need to create a Java model class with class fields that match property key.
In the above application.properties file we have the following properties:
- database.name,
- database.url,
- database.username,
- database.password.
Each property has a prefix “database”. This prefix will need to be specified in a @ConfigurationProperties() annotation. Here is how to create a Java class:
@ConfigurationProperties("database") public class DatabaseConfiguration { private String name; private String url; private String username; private String password; 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 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; } }
Now we can Autowire/Inject this class annotated with @ConfigurationProperties into our Controller class for example and be able to access values in application properties file.
@RestController @RequestMapping(path="/users") public class UsersController { @Autowired DatabaseConfiguration databaseConfiguration; @GetMapping(path="/status/check") public String getStatus() { return " Working with database name " + databaseConfiguration.getName(); } }
Reading a Specific Properties File with @PropertySource
By default, @ConfigurationProperties will work with a default application.properties file in your Spring Boot Application. You can specify which configuration properties file to use by using a @PropertySource annotation.
For example, let’s assume we have the following configuration properties defined in a properties file called dbconfig.properties.
database.name = photo_app database.url = jdbc:mysql://localhost:3306/${database.name} database.username = "developer" database.password = "12345678"
To map properties stored in a dbconfig.properties file we will create the following Java class. Please have a look at how the @PropertySource annotation is used.
@PropertySource("classpath:dbconfig.properties") @ConfigurationProperties("database") public class DatabaseConfiguration { private String name; private String url; private String username; private String password; 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 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; } }
Validating Properties
When using @ConfigurationProperties it is possible to validate the value of the property when it is being set. For example:
@NotEmpty – The annotated element must not be null nor empty,
@Min(5) – The annotated element must be a number whose value must be higher or equal to the specified minimum,
@Max(50) – The annotated element must be a number whose value must be lower or equal to the specified maximum,
@Length(max = 8, min = 16) – Validate that the string is between min and max included,
@Pattern(regexp = “[a-z-A-Z]*”, message = “Username can not contain invalid characters”) – The annotated {@code CharSequence} must match the specified regular expression,
@Email – The string has to be a well-formed email address.
And here is an example of how you would use validation annotations in our class:
@PropertySource("classpath:dbconfig.properties") @ConfigurationProperties("database") public class DatabaseConfiguration { @Length(min=2, max=10) private String name; @NotEmpty private String url; @Length(min=5, max=10) @Pattern(regexp = "[a-z-A-Z]*", message = "Username can not contain invalid characters") private String username; @Length(min=8, max=16) private String password; 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 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; } }
I hope this tutorial was helpful for you. If you are interested to learn more about Spring Boot, have a look at the below list of video courses. One of them might be exactly what you are looking for.