This post demonstrates how to set all the bean properties with values from the .properties file using Spring Boot. We are not going to set them one at a time. We want to set them all simultaneously using the following Spring annotations.
1 | @EnableConfigurationProperties |
1 | @ConfigurationProperties |
Use Spring Initializr
For off, we’ll use Spring Initializr to generate our initial codes. You may refer to Create a Spring Boot Application using IntelliJ. For this post, you may select no dependencies in step 3.
Update pom.xml
Modify your pom.xml to include the following dependency.
1 2 3 4 5 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> |
Update Main class with @EnableConfigurationProperties
You have to annotate your main class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.turreta.config2pojo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; @SpringBootApplication @EnableConfigurationProperties public class ComTurretaConfig2pojoApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(ComTurretaConfig2pojoApplication.class, args); FirstConfig config = (FirstConfig)context.getBean("firstConfig"); System.out.print(config); } } |
Annotate bean with @Component and @ConfigurationProperties
Next, you have to annotate your bean.
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 | package com.turreta.config2pojo; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix="firstconfig") public class FirstConfig { private String appName; private String appVersion; public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } public String getAppVersion() { return appVersion; } public void setAppVersion(String appVersion) { this.appVersion = appVersion; } @Override public String toString() { return "FirstConfig{" + "appName='" + appName + '\'' + ", appVersion='" + appVersion + '\'' + '}'; } } |
The prefix attribute will enable Spring to pick up properties that start with that prefix value.
application.properties
The .properties contains the following properties.
1 2 | firstconfig.appName=Config2Pojo firstconfig.app-version=1.0.2 |
Note that for property names with hyphens or dash (“-“), Spring will convert the following character to uppercase.
firstconfig.app-version is interpreted as firstconfig.appVersion.
Sample Output
Download the Codes
You may download the codes from https://github.com/Turreta/config2pojo