春天如何定义非强制性财产? [英] how to define not mandatory property in spring?
本文介绍了春天如何定义非强制性财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用带有PropertyPlaceholderConfigurator的spring 3.
I'm using spring 3 with PropertyPlaceholderConfigurator.
我的属性代码如下所示:
My properties code looks like as following:
@Configuration
public class MyProps {
@Value("${prop1}")
public String prop1;
...
}
如果我没有在我的.properties文件中,prop1无法初始化它的上下文。
If I do not have a prop1 in my .properties file the spring fails to initialize it's context.
问题是如何定义此属性不是必需的?一些注释,配置?
The question is how can I define that this property is not mandatory? some annotation, configuration?
推荐答案
您可以使用默认值:
@Value("${prop1:}")
public String prop1;
如果未定义属性,spring将注入一个空字符串。语法为 $ {property:defaultValue}
。
and spring will inject an empty string if the property isn't defined. The syntax is ${property:defaultValue}
.
这篇关于春天如何定义非强制性财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文