Injecting null and an empty String

This posts shows how inject null and an empty String as a value of a property of a bean.
package example.di;

public class Example {
    String anEmptyString;
    String nullProperty;
    public void setAnEmptyString(String s){
        this.anEmptyString = s;
    public void setNullProperty(String s){
        this.nullProperty = s;
    public void display(){
        System.out.println("anEmptyString = "+ anEmptyString);
        System.out.println("nullProperty = "+ nullProperty);


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

<beans xmlns=""

<bean id="example" class="example.di.Example">

<!-- Injecting an empty String -->
<property name="anEmptyString" value=""/>

<!-- Injecting null -->
<property name="nullProperty"><null/> </property>

package example.di;

import org.springframework.context.ApplicationContext;

public class test{

public static void main(String args[]){
    ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    Example obj = (Example) appContext.getBean("example");

anEmptyString =
nullProperty = null

I would like to know your comments and if you liked the article then please share it on social networking buttons.

No comments:

Post a Comment