Map Spring Boot properties to a POJO using ConfigurationProperties
Properties files are useful to externalize Spring Boot configuration. This post will discuss how to map all properties defined in the application.properties
file to a POJO using @ConfigurationProperties
.
In the previous post, we have seen how to use the @Value("${property}")
annotation to inject configuration properties. If we’re working with multiple properties, this can be cumbersome. Spring Boot provides a method of working with properties that lets strongly typed beans govern and validate your application’s configuration. This post will discuss how to map all properties defined in the application.properties
file to a POJO.
Consider the following application.properties
file that contains few properties.
1 2 3 4 5 6 |
techiedelight.environment=prod techiedelight.hosts=https://www.techiedelight.com,https://techiedelight.com techiedelight.security.username=user1 techiedelight.security.password=pass1 techiedelight.security.roles=admin,editor,moderator |
Config.java
The following configuration class uses @ConfigurationProperties
annotation to bind and validate the class’s external configuration. Note that the annotation processor will automatically consider inner classes as nested properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; @Configuration @EnableConfigurationProperties @ConfigurationProperties("techiedelight") public class Config { private String environment; private List<String> hosts = new ArrayList<>(); private final Security security = new Security(); public String getEnvironment() { return environment; } public void setEnvironment(String environment) { this.environment = environment; } public List<String> getHosts() { return hosts; } public void setHosts(List<String> hosts) { this.hosts = hosts; } public Security getSecurity() { return security; } public static class Security { private String username; private String password; private List<String> roles = new ArrayList<>(); public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public List<String> getRoles() { return roles; } public void setRoles(List<String> roles) { this.roles = roles; } @Override public String toString() { return "{" + this.getUsername() + ", " + this.getPassword() + ", " + this.getRoles() + "}"; } } @Override public String toString() { return "{" + this.getEnvironment() + ", " + this.getHosts() + ", " + this.getSecurity() + "}"; } } |
Inject @ConfigurationProperties beans
Now that all properties are loaded from an application.properties
file, we can create an object of the Config
class and display the properties using the CommandLineRunner
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Main implements CommandLineRunner { @Autowired private Config config; public static void main(String[] args) { SpringApplication.run(Main.class, args); } @Override public void run(String... strings) throws Exception { System.out.println(config); } } |
We can also inject @ConfigurationProperties
beans in the same way as any other bean, as shown in the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; @Service public class MyService { private final Config properties; @Autowired public MyService(Config properties) { this.properties = properties; } @PostConstruct public void openConnection() { System.out.println(properties); } } |
Output:
{prod, [https://www.techiedelight.com, https://techiedelight.com],
{user1, pass1, [admin, editor, moderator]}}
That’s all about mapping Spring Boot properties to a POJO using @ConfigurationProperties
.
Suggested Read:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)