Introduction
In this article we are going to dive into full details of external properties binding to bean or POJO classes in Spring Boot application. Here we are using .properties file to store properties int external file for example in application.properties file. Property has two components one is property key and another one is the value assign to property..properties mapping to POJO class |
What is application.properties file?
By default application.properties file is added in to classpath of the spring boot application. So simply we can use @ConfigurationProperties file by providing prefix as a parameter then application.properties file will be binding to the model class.application.properties fill we be added to class path automatically
If we want to use .properties file other than application.properties then we have to use @ProeprtySource annotation at class level.
Creating POJO class which is binding with the properties file
As a very first step we need to create a POJO class which annotated with @ConfigurationProperties by providing prefix. Here following is the example of POJO class@Component @ConfigurationProperties(prefix = "msg") public class MessageConfig { String welcome; String location; public String getWelcome() { return welcome; } public void setWelcome(String weclome) { this.welcome = weclome; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } }
In the above class we have used @Component to register this class a bean, and @ConfigurationProperties(prefix="msg") to bind all properties prefixed with msg.
Registering POJO class with @EnableConfigurationProperties
In the above code we have used @Component to register POJO as a bean with Spring Container. Apart from using @Component we can use @EnableConfigurationProperties at a class which annotated with @Configuration. We must provide POJO.class file as a parameter to @EnableConfigurationProperties.@Configuration @EnableConfigurationProperties(MessageConfig.class) public class AppConfig { // rest of the class body here }
Using POJO class to access properties where ever we need
Once POJO class created and registered with Spring Container then we can use it by autowiring POJO class. Here we are using POJO class in controller class by autowiring and accessing by getter methods.
@RestController @RequestMapping("/message") public class MessageController { @Autowired MessageConfig messageConfig; @GetMapping(value ="/m1") public String welcomeMessage() { return messageConfig.getWelcome()+" at " + messageConfig.getLocation(); } }
How to use .properties file other than application.properties file
By default application.properties are loaded into class path of the application. In some situation of application development we have to store properties into another properties file ( other than application.properties ).By using @PropertySource annotation we can use/load .properties file to bind with POJO class
We can create new .properties file under resources folder of application, then to access newly created properties file we can use class path location as a parameter to @ProeprtySource.
messages.properties is the file which stored properties
@Configuration @PropertySource("classpath:messages.properties") ConfigurationProperties(prefix = "msg") public class MessageConfig { String welcome; String location; // getters and setters }
Binding .properties file outside from JAR file
If we want to access .properties file from outside of the application JAR file then we can user file: as a parameter to @PropertySource. This works with in the local system only ( that means we can not access .properties from another server system or from URL )Let us consider location of properties file is D:/messages.properties ( where iam using windows operating system).
@Configuration @PropertySource("file:D:/messages.properties") @ConfigurationProperties(prefix = "msg") public class MessageConfig { String welcome; String location; // getters and setters }
.proeprties file from URL ( http or https url )
We can bind properties from URL ( http or https based protocal ) to POJO class, for example message.properties accessing from https://raw.githubusercontent.com/jai43/spring-example-snippets/master/messages.properties , then we can access proprieties from this remote file into our Spring Boot application.@Configuration @PropertySource("https://raw.githubusercontent.com/jai43/spring-example-snippets/master/messages.properties") @ConfigurationProperties(prefix = "msg") public class MessageConfig { String welcome; String location; // getters and setters }
We must use @PorpertySource annotation with @Configuration class only.
Using POJO class to access properties
Here i have created rest controller endpoint to test binding example@RestController @RequestMapping("/message") public class MessageController { @Autowired MessageConfig messageConfig; @GetMapping(value ="/m1") public String welcomeMessage() { return messageConfig.getWelcome()+" at " + messageConfig.getLocation(); }
Output screens
More than one PropertySource with Same key and value pairs
Here iam giving you an example if we have two PropertySources with same prefixed keys, then what will happen@Configuration @PropertySource("https://raw.githubusercontent.com/jai43/spring-example-snippets/master/messages.properties") @PropertySource("classpath:messages.properties") @ConfigurationProperties(prefix = "msg") public class MessageConfig { String welcome; String location; // getters and setters }
In the above code we have used two property sources , one is from URL and another one is from class path,
Spring container try to find out mapping keys from bottom to top id there is more than one property sources, that means keys can be mapped from bottom to top finding. For more details you can see the above figure, where URL based property file followed by class path property file with same keys , but output took from bottom properties file.
@Value() to map properties file key with POJO class variable
There is no rule that Property Key name must be same as POJO class variable name. We have seen example in the above section with same key name and POJO class variable name. But here we are using @Value annotation to map no same key and variable name. For example location is the key name with in property file with prefix msg, location1 is the variable name in POTO class, then here following code shows you how to map it.@Configuration @PropertySource("https://raw.githubusercontent.com/jai43/spring-example-snippets/master/messages.properties") @PropertySource("classpath:messages.properties") @ConfigurationProperties(prefix = "msg") public class MessageConfig { String welcome; @Value("${msg.location}") String location1; // getters and setters }
Post a Comment
Post a Comment