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

	<relativePath/> <!-- lookup parent from repository -->

1. Binding values form the application.properties file

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

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

# Grouped propeties 
user.name=Atul Rai
[email protected]

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.

package org.websparrow.config;

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

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

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

	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.

  name: Config Annotation
  desc: This is Spring Boot Project

# Grouped propeties 
  id: manish
  name: Manish
  password: [email protected]
  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.

[email protected]

And the respective POJO looks like:

package org.websparrow.config;

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

@ConfigurationProperties(prefix = "db")
public class DBConfiguration {

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

	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.

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;

public class MyController {

	private UserConfiguration userConfiguration;

	private DBConfiguration dbConfiguration;

	public String userConfiguration() {

		return userConfiguration.toString();

	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, [email protected], role=ADMIN]

2. http://localhost:8080/database

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

DBConfiguration [port=34, host=, user=root, [email protected]]


  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.