In this tutorial you will learn about SPEL Wiring Null
In this tutorial you will learn about SPEL Wiring NullSpring 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.
MyClass.java: 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;
}
}
Person.java: This class contains the name of the person.
package spel.wiringnull;
public class Person
{
private String name;
public void setName(String name) {
this.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="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="myClass" class="spel.wiringnull.MyClass">
<property name="person"><null/></property>
</bean>
</beans>
AppMain.java: This class loads the context configuration file spring-beans-list.xml from the class path.
package spel.wiringnull;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
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("-------------------------------------");
System.out.println("Person Object in MyClass: "+myObj.getPerson());
System.out.println("-------------------------------------");
}
}
The output of the above program will be as below:
-------------------------------------
Person Object in MyClass: null
-------------------------------------