I have the following field annotated with @Value
, specifying a default value:
@Value(\"${tolerance.percentage:25}\")
private int tolerance;
Validation using regular validation API annotations is only going to work in certain circumstances.
So instead of using @Value
with those you probably want to create a class that contains the expected properties and use binding with @ConfigurationProperties
. (and you might want to use @Range
instead).
@ConfigurationProperties(prefix="tolerance")
public ToleranceProperties {
@Range(min=1, max=100)
private int percentage = 25;
// Here be a getter/setter
}
This combined on a @Configuration
class add @ EnableConfigurationProperties(ToleranceProperties.class)
and you can use it anywhere you need properties. (See typesafe configuration properties in the reference guide.
Note: You could also declare it as a @Component
.