I'm trying to define the default value as null value in application.yml with SpringBoot version 1.3.0.RELEASE. The goal is be able to refer it with a class with ConfigurationProperties
annotation
-- application.yml --
test.foo: ${test.bar:#{null}}
but it doesn't work.
If the value of test.bar
is not defined, set test.foo
to null (default value)
I already have spring-el in my dependencies. I don't want to use PropertyPlaceholderConfigurer.setNullValue
It's seem to work in @Value
but not in application.yml
(see http://farenda.com/spring/spring-inject-null-value/)
It's a bug, or yaml is not designed for that? I tried all values in http://yaml.org/type/null.html but it doesn't worked either
Thanks
If no default value is declared explicitly, the default value is the null value. This usually makes sense because a null value can be considered to represent unknown data. In a table definition, default values are listed after the column data type.
To read from the application. yml or property file : The easiest way to read a value from the property file or yml is to use the spring @value annotation.
null means that some column is empty/has no value, while default value gives a column some value when we don't set it directly in query.
From the document, we can see:
A common use case is to assign default field values using "#{systemProperties.myProp}" style expressions.
It is actually three phases here:
AutowiredAnnotationBeanPostProcessor
Environmen
ExpressionParser
If we define a test.foo
in application.yml
like you have described:
-- application.yml --
test.foo: ${test.bar:#{null}}
Then, we refer to it in code via @Value
, it works as you said.
@Autowired
public A(@Value("test.foo") Object a) {}
But If we get it directly from environment
, it will be plain string:
env.getProperty("test.foo")
So, whether it is work or not depends on how did you use it. I can't provide more help before more info. Hope following related post may help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With