在Spring中将默认属性值指定为NULL

Ond*_*zek 31 null spring properties default-value

我想在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>
...
Run Code Online (Sandbox Code Playgroud)

这不起作用.甚至可以null在Spring XML配置中定义属性的默认值吗?

Ant*_*lov 85

最好以这种方式使用Spring EL

<property name="password" value="${email.password:#{null}}"/>
Run Code Online (Sandbox Code Playgroud)

它检查是否email.password指定并将其设置为null(不是"null"String)

  • 惊人的!对于那些不知道的人来说,这也适用于 Spring 注解。@Value("${email.password:#{null}}") 私有字符串密码; (2认同)