By using element, you can assign null values to the properties.
Example
id="osho" class="com.sample.pojo.Author"> name="firstName" value="Chandra Mohan" /> name="lastName" value="Jain" /> name="country" value="India" /> name="dateOfBirth"> />
Notice above snippet, it passes null to the property dateOfBirth.
Following is the complete working application.
Author.java
package com.sample.pojo;
public class Author {
private String firstName;
private String lastName;
private String dateOfBirth;
private String country;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
.append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
return builder.toString();
}
}
myConfiguration.xml
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">id="osho" class="com.sample.pojo.Author"> name="firstName" value="Chandra Mohan" /> name="lastName" value="Jain" /> name="country" value="India" /> name="dateOfBirth"> />
HelloWorld.java
package com.sample.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sample.pojo.Author;
public class HelloWorld {
public static void main(String args[]) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });
Author author = context.getBean("osho", Author.class);
System.out.println(author);
((ClassPathXmlApplicationContext) context).close();
}
}
Run HelloWorld.java, you can able to see following output.
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=null, country=India]
Previous Next Home
This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here