Blog Image

Example On NULL Tag In Spring  

Here we are going to see how to use spring provided <NULL> tag to inject null value in a target bean.

While performing setter or constructor injections some time we may have to inject some NULL values to some of the attribute. If you directly put "null" via constructor or setter injection spring will treat it as a String.
For example: <construstor-arg>null</constructor-arg> spring inject "null" as a string to the target bean.
So to inject NULL value spring has provided a <NULL> tag.

Let us See the example on <Null> and how we can use <Null> tag to inject NULL value in our target beans lets explore.

Technologies used :-
 1) java (1.8).
 2) maven (3.5.2).
 3) Spring (4.0.0).

Project Structure

Execute below command for creating maven project.
mvn archetype:generate -DgroupId=com.sjg
-DartifactId=example-on-null
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

Below is the maven project structure with packages and java src files.

Classes
Person.java
package com.sjg.beans;

public class Person {
    private Passport passport;

    public Person(Passport passport) {
        this.passport = passport;
    }

    @Override
    public String toString() {
        return "Person [passport=" + passport + "]";
    }

}
Passport.java
package com.sjg.beans;

public class Passport {
    private String passportNo;
    private String issuedDate;

    public String getPassportNo() {
        return passportNo;
    }

    public void setPassportNo(String passportNo) {
        this.passportNo = passportNo;
    }

    public String getIssuedDate() {
        return issuedDate;
    }

    public void setIssuedDate(String issuedDate) {
        this.issuedDate = issuedDate;
    }

    @Override
    public String toString() {
        return "Passport [passportNo=" + passportNo + ", issuedDate="
                + issuedDate + "]";
    }

}
application-context.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";
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">;

    <bean id="person" class="com.sjg.beans.Person">
        <constructor-arg>
            <null />
        </constructor-arg>
    </bean>

</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">;
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sjg</groupId>
  <artifactId>example-on-null</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>example-on-null</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.srimanjavagroup.com</url>;

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>
    </dependencies>
</project>
Test.java
package com.sjg.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.sjg.beans.Person;

public class Test {
    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/sjg/common/application-context.xml"));
        Person person = factory.getBean("person", Person.class);
        System.out.println(person);
    }

}

Please find attached the example_on_null.zip as an maven project as a reference. Feel free to pass your comments in the blog.

About author

User Image
Sriman

A software developer on JDK and JEE platform. I am passionate about Java technology and always an explorer and learner in new technologies in Java. I have experience on Open Source technologies like Struts, Spring, Jsf etc.. and even strong knowledge on Integration/Distributed world like Ejb, Web Services and Restful Services.

0

-Comments

Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.