6

这是我上下文的一小部分:

<property name="a" value="1"/> where a is Integer. 

如何将 null 设置为此值?

4

2 回答 2

12

您可以使用该元素<null/>来指示空值:

<property name="a" value="1"/><null/></property>

编辑:官方 spring 2.5 文档中有更多信息:http: //static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-null-element

于 2012-05-29T14:28:31.710 回答
2

在 Spring 配置文件中有设置 null 值的方法。

春天:

<bean class="SampleBean">
    <property name="name"><value></value></property>
</bean>

结果 name 属性被设置为 "",相当于 java 代码:sampleBean.setName("")。特殊<null>元素可用于指示空值,因此:

春天:

<bean class="ExampleBean">
    <property name="email"><null/></property>
</bean>

以上配置等价于java代码:

爪哇:

exampleBean.setEmail(null).

请参阅此链接:http ://www.java-forums.org/java-tip/3218-how-set-null-value-springs-configuration-file.html

于 2012-05-29T14:31:18.480 回答