How to define default null value in application.yml in Spring Boot

回眸只為那壹抹淺笑 提交于 2019-12-03 02:55:49

@Value

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:

  • Spring get the annotation string -- AutowiredAnnotationBeanPostProcessor
  • Spring get property value by the annotation meta data -- Environmen
  • Spring interpret the property value -- ExpressionParser

User Case

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")

More

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!