Spring – Configurable Default Value for @Value
that has both a configurable default value and a hard-coded final fallback value. This annotation may be used to inject values into fields in Spring-managed beans.
Requirements
Stuff used in this post.
- Java 8 (JDK)
- IntelliJ IDEA Ultimate 2016.3
- Spring Boot 1.5.6.RELEASE
@Value General Usage
@Value
can be applied at the field or constructor/method parameter level.
1 2 3 4 5 6 7 8 | @Component public class GeneralUsageBean { @Value("${from.configuration.file.property.a}") private String propertyA; ... } |
1 2 3 4 5 6 7 8 9 | @Component public class GeneralUsageBean { public GeneralUsageBean( @Value("${from.configuration.file.property.a}") String propertyA) { ... } } |
Hard-coded Default Value
It is possible to assign a field or constructor/method parameters with default value when, e.g., the property from.configuration.file.property.a
is not defined.
1 2 3 4 5 6 7 | @Component public class GeneralUsageBean { @Value("${from.configuration.file.property.a: MY DEFAULT VALUE}") private String propertyA; ... } |
From these usages, let’s go beyond them!
Instead of just having a hard-coded default value, we’ll have a configurable default value and a hard-coded final fallback default value.
Our application.properties
Initially, our configuration file has the following lines.
1 2 | mycomponent.user.property.limit=10 mycomponent.system.property.limit=100 |
One property is “user-defined” and another is “system-defined”.
We use these properties with the @Value
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 | package com.turreta.springboot.property.configurable.defaultval; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class SomeLimitBean { /* * If mycomponent.user.property.limit is not defined, * use mycomponent.system.property.limit. * * If mycomponent.system.property.limit is also not defined, * use 200. * */ @Value("${mycomponent.user.property.limit:${mycomponent.system.property.limit:200}}") private Integer someNumericLimit; public Integer getSomeNumericLimit() { return someNumericLimit; } } |
Notice the expression passed to the @Value
annotation.
Testing
We’ll test 3 scenarios: 1) with “user-defined” and “system-defined” properties defined; 2) with only “system-defined” property defined; and finally, 3) with no properties explicitly defined.
Test A
Configuration:
1 2 | mycomponent.user.property.limit=10 mycomponent.system.property.limit=100 |
Output:
It outputs the “user-defined” value.
Test B
Configuration:
1 2 | #mycomponent.user.property.limit=10 mycomponent.system.property.limit=100 |
Output:
It outputs the configurable default value.
Test C
Configuration:
1 2 | #mycomponent.user.property.limit=10 #mycomponent.system.property.limit=100 |
Output:
It outputs the hard-coded final fallback value.
Download the codes
Please download from our GitHub account.
https://github.com/Turreta/4uq0ofsm1rj2xgedpf6b