The Spring boot configuration properties example shows you how to use @ConfigurationProperties annotation loading the entire file into a object easily. Beside, we also show you the JSR-303 bean validation that @ConfigurationProperties annotation supports in a Spring application. This example will introduce about the @Value annotation then we will get the right way to use @Value annotation or @ConfigurationProperties annotation in your application.
Other interesting posts you may like
Let’s begin:
Using @Value and @ConfigurationProperties annotation
For example, we have a simple property file named system.properties:
1 2 |
email=test@javabycode.com pool-size=4 |
We can use the @Value annotation to inject the property value one by one, this is good option for simple structure property file.
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 |
package com.javabycode; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:system.properties") public class SystemProperties { @Value("${pool-size}") private int poolSize; @Value("${email}") private String email; public int getPoolSize() { return poolSize; } public void setPoolSize(int poolSize) { this.poolSize = poolSize; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "SystemProperties{" + "poolSize=" + poolSize + ", email='" + email + '\'' + '}'; } } |
In case, using @ConfigurationProperties annotation:
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 |
package com.javabycode; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @ConfigurationProperties public class SystemProperties { private int poolSize; private String email; public int getPoolSize() { return poolSize; } public void setPoolSize(int poolSize) { this.poolSize = poolSize; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "SystemProperties{" + "poolSize=" + poolSize + ", email='" + email + '\'' + '}'; } } |
Using @ConfigurationProperties for the Complex Properties file
The @ConfigurationProperties annotation supports both .properties file and YAML file.
For example, we have a complex structure property file named application.properties, below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#Logging logging.level.com.javabycode.springframework.web=ERROR logging.level.com.javabycode=DEBUG #System email=test@javabycode.com pool-size=3 #App app.menus[0].label=Home app.menus[0].path=/ app.menus[1].label=Java Core app.menus[1].path=/javacore app.menus[2].label=Spring app.menus[2].path=/spring app.error404=/error-404/ |
or we can have also the equilvent in YAML named application.yml, below
1 2 3 4 5 6 7 8 9 10 11 12 13 |
logging: level: org.springframework.web: ERROR com.javabycode: DEBUG email: test@javabycode.com pool-size: 3 app: menus: - label: Home path: / - label: Login path: /login error404: /error-404/ |
Next, create a bean which is annotated with @ConfigurationProperties annotation, like this:
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 |
package com.javabycode; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component @ConfigurationProperties("app") public class AppProperties { private String error404; private List<Menu> menus = new ArrayList<Menu>(); public static class Menu { private String path; private String label; public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } @Override public String toString() { return "Menu{" + "path='" + path + '\'' + ", label='" + label + '\'' + '}'; } } public String getError404() { return error404; } public void setError404(String error404) { this.error404 = error404; } public List<Menu> getMenus() { return menus; } public void setMenus(List<Menu> menus) { this.menus = menus; } @Override public String toString() { return "AppProperties{" + "error404='" + error404 + '\'' + ", menus=" + menus + '}'; } } |
Create Spring Boot Application
In this Spring boot configuration properties example. Our Spring Boot Application class is simple like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javabycode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootWebApplication { public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } } |
Create web controller
In this Spring boot configuration properties example, we create a controller to see how @Value and @ConfigurationProperties annotations work perfectly:
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 |
package com.javabycode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Map; @Controller public class MyController { private static final Logger logger = LoggerFactory.getLogger(MyController.class); private AppProperties app; private SystemProperties global; @Autowired public void setApp(AppProperties app) { this.app = app; } @Autowired public void setGlobal(SystemProperties global) { this.global = global; } @RequestMapping("/") public String home(Map<String, Object> model) { String appProperties = app.toString(); String globalProperties = global.toString(); logger.debug("Home {}, {}", app, global); model.put("globalProperties",globalProperties); model.put("appProperties", appProperties); return "home"; } } |
Run the Spring Boot Web Application: Don’t forget go to the project directory and run the command line mvn spring-boot:run. Then access controller via url http://localhost:8080/, review the output screen:
@ConfigurationProperties Validation
The @ConfigurationProperties annotation also supports JSR-303 bean validation – javax.validation, for example:
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 |
package com.javabycode; import org.hibernate.validator.constraints.NotEmpty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import javax.validation.constraints.Max; import javax.validation.constraints.Min; @Component @ConfigurationProperties public class SystemProperties { @Max(3) @Min(0) private int poolSize; @NotEmpty private String email; public int getPoolSize() { return poolSize; } public void setPoolSize(int poolSize) { this.poolSize = poolSize; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "SystemProperties{" + "poolSize=" + poolSize + ", email='" + email + '\'' + '}'; } } |
Now, we try to run the command line mvn spring-boot:run to start Spring Boot Application again. The console will print the error like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
*************************** APPLICATION FAILED TO START *************************** Description: Binding to target SystemProperties{poolSize=3, email='test@javabycode.com'} fail ed: Property: target.poolSize Value: 3 Reason: must be less than or equal to 2 Action: Update your application's configuration |
That’s all on the Spring boot configuration properties example.
References
ConfigurationProperties Docs
JSR 303: Bean Validation
Download complete source code, click link below
spring-boot-configuration-properties-example.zip (287 downloads)
hi!