@Value("${some.setting:8}") private int mySetting;
So here, we wanted a default value of
8
if the some.settings
property is not found. Simple enough, but still... you end up getting this kind of error:org.springframework.beans.factory.BeanCreationException: Error creating bean with name '.... blah blah blah ...'
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private int com.foo.MyBean.mySetting; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type [java.lang.String] to required type [int]; nested exception is java.lang.NumberFormatException: For input string: "${some.setting:8}" ... Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type [java.lang.String] to required type [int]; nested exception is java.lang.NumberFormatException: For input string: "${some.setting:8}" ... Caused by: java.lang.NumberFormatException: For input string: "${some.setting:8}"This means that Spring does not know how to interpret the default value expression. To enable the Spring Expression Language in
@Value
just add PropertySourcesPlaceholderConfigurer
to the configuration.
In Java annotations:
@Configuration public class MyConfig { ... @Bean public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } ... }In XML, this is usually not a problem because you've got:
<context:property-placeholder location="classpath:defaults.properties"/>
Thanks to MyKong for the solution!
See http://www.mkyong.com/spring3/spring-value-default-value/
No comments:
Post a Comment