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.
org.name1=John org.name2=Paul org.name3=Neil
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.
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.
@Configuration @ConfigurationProperties(prefix = "org") public class AppProps { String name1; String name2; String name3; // Getters and Setters }
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).
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.
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.
And the names John, Paul and Neil are taken from the properties file and placed in the attributes of AppProps class.
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.
@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()); } }
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
And if you see the application console, three names are printed on the Screen.