Share on Google+Share on Google+

SPEL-Wiring Null

In this tutorial you will learn about SPEL Wiring Null

SpEL: Wiring Null

Spring 3 provides powerful Expression Language which can be used to wire a property to null . Lets take an example to demonstrate how <null/> element of SpEL is used to ensure that a property is null. The MyClass class contains property named "person" which is of Person type.

package spel.wiringnull;

import spel.wiringnull.Person;

public class MyClass
private Person person;

public void setPerson(Person person) {
this.person = person;
public Person getPerson() {
return person;
} This class contains the name of the person.

package spel.wiringnull;

public class Person
private String name;

public void setName(String name) { = name;
public String getName() {
return name;

spring-beans.xml: Using Spring expression language the property person of MyClass bean is wired with the null value using <null/> element.

<beans xmlns=""

<bean id="myClass" class="spel.wiringnull.MyClass">
<property name="person"><null/></property>

</beans> This class loads the context configuration file spring-beans-list.xml from the class path.

package spel.wiringnull;

import org.springframework.context.ApplicationContext;

import spel.wiringnull.MyClass;

public class AppMain{
public static void main( String[] args ){

ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {"spel\\wiringnull\\spring-beans-list.xml"});
MyClass myObj = (MyClass)appContext.getBean("myClass");

System.out.println("Person Object in MyClass: "+myObj.getPerson());

The output of the above program will be as below:

Person Object in MyClass: null

Download Code


Posted on: April 8, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.