Kev*_*idt 83 java spring spring-annotations
我目前正在使用@Value Spring 3.1.x注释,如下所示:
@Value("${stuff.value:}")
private String value;
Run Code Online (Sandbox Code Playgroud)
如果属性不存在,这会将空字符串放入变量中.我想将null作为默认值而不是空字符串.当然,我还想避免在未设置属性stuff.value时出错.
小智 60
您必须设置PropertyPlaceholderConfigurer的nullValue .对于示例,我使用的是字符串,@null
但您也可以将空字符串用作nullValue.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- config the location(s) of the properties file(s) here -->
<property name="nullValue" value="@null" />
</bean>
Run Code Online (Sandbox Code Playgroud)
现在您可以使用该字符串@null
来表示该null
值
@Value("${stuff.value:@null}")
private String value;
Run Code Online (Sandbox Code Playgroud)
请注意:上下文名称空间目前不支持空值.你不能使用
<context:property-placeholder null-value="@null" ... />
Run Code Online (Sandbox Code Playgroud)
用Spring 3.1.1测试
Kir*_* Ch 28
感谢@vorburger:
@Value("${email.protocol:#{null}}")
String protocol;
Run Code Online (Sandbox Code Playgroud)
将字符串值设置为null而不进行任何其他配置.
归档时间: |
|
查看次数: |
59677 次 |
最近记录: |