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

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


package com.sjg.beans;

public class Person {
    private Passport passport;

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

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


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;

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



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="";

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



<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=""; xmlns:xsi="";


    <!-- FIXME change it to the project's website -->


package com.sjg.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
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);


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

About author

User Image

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.



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.