How to determine default null in application.yml in Spring

I am trying to define the default value as null in application.yml with SpringBoot version 1.3.0.RELEASE. The goal is to pass it to the class with the ConfigurationProperties annotation

 -- application.yml -- test.foo: ${test.bar:#{null}} 

but that will not work.

If test.bar not defined, set test.foo to null (default value)

I already have spring -el in my dependencies. I do not want to use PropertyPlaceholderConfigurer.setNullValue

It seems to work in @Value , but not in application.yml (see http://farenda.com/spring/spring-inject-null-value/ )

Is this a mistake, or is Yamal not intended for this? I tried all the values ​​in http://yaml.org/type/null.html , but that didn't work.

thanks

+6
source share
1 answer

@value

The document shows that

A common use case is to assign default values ​​using the style expressions "# {systemProperties.myProp}".

These are actually three phases:

  • Spring get string annotations - AutowiredAnnotationBeanPostProcessor
  • Spring get property value from annotation metadata - Environmen
  • Spring interpret property value - ExpressionParser

User case

If we define a test.foo in application.yml , as you described:

 -- application.yml -- test.foo: ${test.bar:#{null}} 

Then we refer to it in the code via @Value , it works as you said.

 @Autowired public A(@Value("test.foo") Object a) {} 

But if we get it directly from the environment , it will be a simple line:

 env.getProperty("test.foo") 

More details

So, regardless of whether it works or not, it depends on how you used it. I can no longer provide additional information. Hope the following related post might help.

+1
source

All Articles