Wednesday, 13 April 2011

Setting Null Values in Spring Config

There are some occasions when you need the ensure that null values are passed to objects defined in Spring XML. Now, with you own objects you don’t usually need to bother with this as you can design then to ensure that attributes are initialised to null during their creation.

If you can’t ensure this in code, then you can use the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="basic" class="spring.nullvalue.example.NullBean">
    <property name="message">
      <null />
    </property>
  </bean>

</beans>

No comments: