6

this is small part of my context:

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

How I can set null to this value ?

hudi
  • 15,555
  • 47
  • 142
  • 246
  • possible duplicate of [Intentionally setting a Spring bean to null](http://stackoverflow.com/questions/2163182/intentionally-setting-a-spring-bean-to-null) – jmj May 29 '12 at 14:39
  • @JigarJoshi - That question is asking how to set a bean to null. This one is asking how to set a property to null. – Paul Bellora May 29 '12 at 14:54

2 Answers2

12

You can use the element <null/> to indicate a null value:

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

Edit: There is more information in the official spring 2.5 documentation here: http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-null-element

Mesop
  • 5,187
  • 4
  • 25
  • 42
  • 1
    Add a [link](http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-null-element) would be good. – Pau Kiat Wee May 29 '12 at 14:30
  • +1 http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-null-element – jmj May 29 '12 at 14:31
2

There is the way to set the null value in the Spring configuration file.

Spring:

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

Results in the name property being set to "", equivalent to the java code: sampleBean.setName(""). The special <null> element may be used to indicate a null value, so that:

Spring:

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

The above configuration is equivalent to the java code:

Java:

exampleBean.setEmail(null).

See this link: http://www.java-forums.org/java-tip/3218-how-set-null-value-springs-configuration-file.html

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
  • 5
    You just copied and pasted this from http://www.java-forums.org/java-tip/3218-how-set-null-value-springs-configuration-file.html Please cite your sources. – Paul Bellora May 29 '12 at 14:34