Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforce constraints on @Value annotated field in Spring Boot application

I have the following field annotated with @Value, specifying a default value:

@Value("${tolerance.percentage:25}")
private int tolerance;

That code correctly initializes the value of the field to the system property "tolerance.percentage" if that prop exists. If it doesn't exist, it defaults to 25.

I want to go one step further, though, by enforcing a min and max on this int field, since it represents a percentage less than 100 as a whole number, and Murphy's law means someone (probably me) can externally misconfigure the property and my app would start doing weird things at runtime, which is way too late for my liking. I would like an error to be thrown if the property is set to "101" or "-1" upon application startup. Heck, I'd even like for an error to be thrown if I try to default it to 101 in the @Value annotation, but that's not important for the purposes of this question. Here's what I tried:

//@Min and @Max don't produce the intended behavior when combined with @Value
@Min(0)
@Max(100)
@Value("${tolerance.percentage:25}")
private int tolerance;

Can I enforce a min and max on an int field that @Value is aware of?

like image 757
JellyRaptor Avatar asked May 16 '17 17:05

JellyRaptor


People also ask

What does @value annotation do in spring boot?

Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. Spring @Value annotation also supports SpEL.

What does the @value annotation do?

@Value is a Java annotation that is used at the field or method/constructor parameter level and it indicates a default value for the affected argument. It is commonly used for injecting values into configuration variables - which we will show and explain in the next part of the article.

What is @configuration in spring boot?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.


1 Answers

Validation using regular validation API annotations is only going to work in certain circumstances.

  1. You have an implementation ('hibernate-validator') on the classpath
  2. The class they are in are used to bind externalized configuration

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.

like image 101
M. Deinum Avatar answered Oct 19 '22 04:10

M. Deinum