@ConfigurationProperties Annotation in Spring Boot


@ConfigurationProperties annotation is used to pulling up a “group of configuration values” from the properties file. @ConfigurationProperties annotation is also used for externalized configuration while developing a microservice architecture based application.

It is all most similar to @Value annotation that we have discussed in one of the previous Spring Boot blogs.

@ConfigurationProperties vs @Value Annotation

The main differences in @ConfigurationProperties annotation and @Value annotation are:

  1. @ConfigurationProperties annotation is used to inject a similar group (prefix) of values from properties file while @Value annotation injects the single/specific value.
  2. @ConfigurationProperties is to map properties with POJO beans and @Value is to inject a particular property value by it’s key.

Dependencies Required

There is no special dependencies are required to work with @ConfigurationProperties annotation. spring-boot-starter-parent is enough.

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.2.4.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

1. Binding values form the application.properties file

Here is the application.properties file where we have grouped user details with prefix user.

application.properties
app.name=Config Annotation
app.desc= This is Spring Boot Project

# Grouped propeties 
user.id=atul
user.name=Atul Rai
user.password=Hello@123
user.role=ADMIN

Now let’s create the respective POJO class in which these properties values will inject. @ConfigurationProperties annotation has an attribute i.e. prefix where we can map our properties key.

UserConfiguration.java
package org.websparrow.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "user")
public class UserConfiguration {

	// Generate Getters and Setters...
	private String id;
	private String name;
	private String password;
	private String role;

	@Override
	public String toString() {
		return "UserConfiguration [id=" + id + ", name=" + name + ", password="
				+ password + ", role=" + role + "]";
	}

}

Note: If we don’t use @Configuration in the POJO, then we need to add @EnableConfigurationProperties(UserConfiguration.class) in the main Spring application class to bind the properties into the POJO.

2. Binding values form the application.yaml file

As similar to the application.properties file, we can also inject the properties values form application.yaml file also.

application.yaml
app:
  name: Config Annotation
  desc: This is Spring Boot Project

# Grouped propeties 
user:
  id: manish
  name: Manish
  password: abc@123
  role: USER

3. Binding values form any properties file

We can inject the values form any properties file(database.properties). In this case, we will use the @PropertySource annotation to load the properties file.

For this here the properties file which holds all the database credentials.

database.properties
db.port=34
db.host=127.0.0.1
db.user=root
db.password=admin@123

And the respective POJO looks like:

DBConfiguration.java
package org.websparrow.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:database.properties")
@ConfigurationProperties(prefix = "db")
public class DBConfiguration {

	// Generate Getters and Setters
	private int port;
	private String host;
	private String user;
	private String password;

	@Override
	public String toString() {
		return "DBConfiguration [port=" + port + ", host=" + host + ", user="
				+ user + ", password=" + password + "]";
	}
}

Test the application

To the application whether it is pulling the values from the properties file or not, we have created MyController class and autowired both configurations class using @Autowired annotation.

MyController.java
package org.websparrow.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.websparrow.config.DBConfiguration;
import org.websparrow.config.UserConfiguration;

@RestController
public class MyController {

	@Autowired
	private UserConfiguration userConfiguration;

	@Autowired
	private DBConfiguration dbConfiguration;

	@GetMapping("/config")
	public String userConfiguration() {

		return userConfiguration.toString();
	}

	@GetMapping("/database")
	public String databaseConfiguration() {

		return dbConfiguration.toString();
	}
}

Here, we are done 🙂 Just start your application and hit below end-points in your favorite browser:

1. http://localhost:8080/config

It will inject the values from the application.properties file grouped by user.

UserConfiguration [id=atul, name=Atul, password=Hello@123, role=ADMIN]

2. http://localhost:8080/database

It will inject the values from the database.properties file grouped by db.

DBConfiguration [port=34, host=127.0.0.1, user=root, password=admin@123]

References

  1. Spring @Value Annotation Example
  2. Spring @Autowired annotation example
  3. Spring 5 @Qualifier annotation example
  4. Externalized Configuration- Spring Boot

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.