【发布时间】:2013-05-20 01:33:47
【问题描述】:
我想在 Spring XML 配置文件中定义默认属性值。我希望这个默认值是null。
类似这样的:
...
<ctx:property-placeholder location="file://${configuration.location}"
ignore-unresolvable="true" order="2"
properties-ref="defaultConfiguration"/>
<util:properties id="defaultConfiguration">
<prop key="email.username" >
<null />
</prop>
<prop key="email.password">
<null />
</prop>
</util:properties>
...
这不起作用。是否可以在 Spring XML 配置中为属性定义 null 默认值?
【问题讨论】:
-
您回答了自己的问题。使用
-
这样不行:
Invalid content starting with element 'null'. No child element is expected at this point. -
我认为你做不到。也许删除这些属性就足够了。如果未定义属性,Java Properties 将返回 null。
-
如果属性文件中缺少属性并且我没有定义默认值,当我尝试引用这个缺少的属性时会出现问题:
Could not resolve placeholder 'email.username' in string value "${email.username}" -
您可以使用 ${email.username:null} 将 null 设置为默认值,尽管这可能会给您字符串“null”。或者,如果你使用@Value,你可以添加@Autowired(required = false),那么任何缺少的属性都将被设置为null。
标签: spring properties null default-value