Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly specify a default value in the Spring @Value annotation?

Initially, I have the following spec:

@Value("#{props.isFPL}") private boolean isFPL=false; 

This works fine correctly getting the value from the property file:

isFPL = true 

However, the following expression with default results in the error:

@Value("#{props.isFPL:false}") private boolean isFPL=false; 

Expression parsing failed; nested exception is org.springframework.expression.spel.SpelParseException: EL1041E:(pos 28): After parsing a valid expression, there is still more data in the expression: 'colon(:)'

I also tried to use $ instead of #.

@Value("${props.isFPL:true}") private boolean isFPL=false; 

Then the default value in annotation works fine but I did not get the correct value from the Properties file:

like image 203
Alex Avatar asked Nov 13 '14 19:11

Alex


People also ask

How do I set default value in Spring?

To set a default value for primitive types such as boolean and int, we use the literal value: @Value("${some. key:true}") private boolean booleanWithDefaultValue; @Value("${some.

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.

How do I set default value in RequestParam?

By default, @RequestParam requires query parameters to be present in the URI. However, you can make it optional by setting @RequestParam 's required attribute to false . In the above example, the since query param is optional: @RequestParam(value="since", required=false) ).

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.


2 Answers

Try with $ as follows:

@Value("${props.isFPL:true}") private boolean isFPL=false; 

Also make sure you set the ignore-resource-no-found to true so that if the property file is missing, the default value will be taken.

Place the following in the context file if using XML based configuration:

<context:property-placeholder ignore-resource-not-found="true"/> 

If using Java configurations:

 @Bean  public static PropertySourcesPlaceholderConfigurer   propertySourcesPlaceholderConfigurer() {      PropertySourcesPlaceholderConfigurer p =  new PropertySourcesPlaceholderConfigurer();      p.setIgnoreResourceNotFound(true);      return p;  } 
like image 122
shi9 Avatar answered Sep 25 '22 12:09

shi9


For int type variable:

@Value("${my.int.config: #{100}}") int myIntConfig; 

Note: there is no space before the colon, but an extra space after the colon.

like image 37
Kevin Liu Avatar answered Sep 24 '22 12:09

Kevin Liu