Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring Boot - @ConfigurationProperties Annotation


Say, you have have a requirement where you are supposed to get data from a properties file.


And let us suppose, there are 3 names John, Paul and Neil. And we are going to get those from the properties file called application.properties.


At first let us save the names in the application.properties file.


application.properties


org.name1=John
org.name2=Paul
org.name3=Neil
Spring Boot - @ConfigurationProperties Annotation

Now, you must be thinking, where does @ConfigurationProperties Annotation comes into picture?


Well! @ConfigurationProperties, as the name suggests, has something to do with Properties and Configuration.


And @ConfigurationProperties annotation is going to read details from the application.properties file.


How does @ConfigurationProperties annotation reads the properties file?


To use @ConfigurationProperties annotation, let us create a class and name it AppProps(However, you can give any name), and annotate it with @ConfigurationProperties annotation.


AppProps.java


@Configuration
@ConfigurationProperties(prefix = "org")
public class AppProps {

	String name1;
	String name2;
	String name3;

	// Getters and Setters
}
Spring Boot - @ConfigurationProperties Annotation

So, we have annotated the AppProps class with @ConfigurationProperties and set the prefix field with org.


@ConfigurationProperties(prefix = "org")

And what Spring Boot does is, checks the prefix (i.e. org) and goes inside the AppProps class.


Just note AppProps class has three properties,


public class AppProps {

	String name1;
	String name2;
	String name3;

	// Getters and Setters
}

Once Spring Boot finds there are three attributes in AppProps class(i.e. name1, name2 and name3). Spring Boot clubs the prefix (i.e. org) with the three attributes (i.e. org.name1, org.name2 and org.name3).

Spring Boot - @ConfigurationProperties Annotation

And now that Spring Boot has the clubbed attributes(i.e. org.name1, org.name2 and org.name3). The next thing it does is, it checks for the application.properties file attributes.


application.properties


org.name1=John
org.name2=Paul
org.name3=Neil

And it finds that application.properties has org.name1, org.name2 and org.name3 with values as John, Paul and Neil.

Spring Boot - @ConfigurationProperties Annotation

And the names John, Paul and Neil are taken from the properties file and placed in the attributes of AppProps class.

Spring Boot - @ConfigurationProperties Annotation

Now, let us see the overall picture.


We need to write a Rest Controller where we can hit the /students endpoint and get the details from application.properties.


HelloWorldController.java


@RestController
public class HelloWorldController {

	@Autowired
	AppProps appProps;

	@RequestMapping(method = RequestMethod.GET, value = "/students")
	public void getStudentDetailsFromPropertiesFile() {

		System.out.println("Name1 : "+appProps.getName1());
		System.out.println("Name2 : "+appProps.getName2());
		System.out.println("Name3 : "+appProps.getName3());
	}
}
Spring Boot - @ConfigurationProperties Annotation

So in HelloWorldController, we have Autowired the AppProps class.


And created a method called getStudentDetailsFromPropertiesFile() method,


@RequestMapping(method = RequestMethod.GET, value = "/students")
public void getStudentDetailsFromPropertiesFile() {

	System.out.println("Name1 : "+appProps.getName1());
	System.out.println("Name2 : "+appProps.getName2());
	System.out.println("Name3 : "+appProps.getName3());
}

In getStudentDetailsFromPropertiesFile() method, we have printed names from the properties file.


Now, let us start the application and hit the /students endpoint using POSTMAN.


localhost:8080/students
Spring Boot - @ConfigurationProperties Annotation

And if you see the application console, three names are printed on the Screen.

Spring Boot - @ConfigurationProperties Annotation