Spring Null value, Set null value in Springs configuration file


 

Spring Null value, Set null value in Springs configuration file

In this example you will see how to set null value in the Spring configuration file

In this example you will see how to set null value in the Spring configuration file

Spring Null value, Set null value in Springs configuration file

In this example you will see how to set null value in the Spring configuration file.

NullBean.java

package spring.nullvalue.example;

public class NullBean {
  private String mesg = "no value passed";

  public void setMessage(String mesg) {
    this.mesg = mesg;
  }

  public String getMessage() {
    return mesg;
  }

}

NullBeanMain.java

package spring.nullvalue.example;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class NullBeanMain {
  public static void main(String[] args) {
    BeanFactory beanfactory = new ClassPathXmlApplicationContext(
        "context.xml");
    NullBean bean = (NullBeanbeanfactory.getBean("basic");
    System.out.println(bean.getMessage());
  }
}

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" 
xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
>
  <bean id="basic" class="spring.nullvalue.example.NullBean">
    <property name="message">
      <null />
    </property>
  </bean>
</beans>

When you run this application it will display output as shown below:

    null

Download this example code

Ads