In Constructor based DI(Dependency Injection), Objects define their dependencies via Constructor argument(<constructor-arg> tag) and Container invoke Constructors to set these arguments in object.
Address Bean Class
package com.jbt.bean;
import java.util.List;
public class Address {
public String flatNo;
public String bldgNo;
public String streetNo;
public String city;
public int pincode;
public String getStreetNo() {
return streetNo;
}
public void setStreetNo(String streetNo) {
System.out.println("Setter method is called");
this.streetNo = streetNo;
}
public String getFlatNo() {
return flatNo;
}
public void setFlatNo(String flatNo) {
this.flatNo = flatNo;
}
public String getBldgNo() {
return bldgNo;
}
public void setBldgNo(String bldgNo) {
this.bldgNo = bldgNo;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getPincode() {
return pincode;
}
public void setPincode(int pincode) {
this.pincode = pincode;
}
Address(String flatNo, String bldgNo, String streetNo, String city,
int pincode) {
super();
System.out.println("Constructor with multiple argument invoked");
this.flatNo = flatNo;
this.bldgNo = bldgNo;
this.streetNo = streetNo;
this.city = city;
this.pincode = pincode;
}
Address(List l1) {
super();
System.out.println("Constructor with Single List argument invoked");
this.flatNo = (String) l1.get(0);
this.bldgNo = (String) l1.get(1);
this.streetNo = (String) l1.get(2);
this.city = (String) l1.get(3);
this.pincode = Integer.parseInt((String) l1.get(4));
}
@Override
public String toString() {
return "Address [flatNo=" + flatNo + ", bldgNo=" + bldgNo
+ ", streetNo=" + streetNo + ", city=" + city + ", pincode="
+ pincode + "]";
}
}
Above Bean Class has two Constructor one with multiple argument for different property while other takes single argument as List.
Spring Configuration File
<?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-3.1.xsd">
<!-- This is constructor injection. Constructor with List argument will
be invoked -->
<bean id="addressList" name="address1,address2" class="com.jbt.bean.Address">
<constructor-arg>
<list>
<value>203</value>
<value>2C</value>
<value>JBT Street</value>
<value>New York</value>
<value>276001</value>
</list>
</constructor-arg>
</bean>
<!-- This is constructor injection. Constructor with multiple argument will
be invoked -->
<bean id="address" primary="true" class="com.jbt.bean.Address">
<constructor-arg type="int" value="276044"></constructor-arg>
<constructor-arg type="java.lang.String" value="My Street"></constructor-arg>
<constructor-arg type="java.lang.String" value="My City"></constructor-arg>
<constructor-arg type="java.lang.String" value="360"
index="0"></constructor-arg>
<constructor-arg type="java.lang.String" value="C2"
index="1"></constructor-arg>
</bean>
</beans>
Execute the Programme
package com.jbt;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jbt.bean.Address;
/*
* Here we will learn to Dependency Inject Bean with Setter Method.
*/
public class SpringDIConstructor {
public static void main(String[] args) {
ApplicationContext cpxac = new ClassPathXmlApplicationContext(
"applicationContext_DIConstructor.xml");
Address address = (Address) cpxac.getBean("address");
/*
* address1 & address2 are the alias name for address id. Alias name can
* be defined in XML file using "name" attribute.
*/
Address address1 = (Address) cpxac.getBean("address1");
Address address2 = (Address) cpxac.getBean("address2");
System.out.println("This is address Bean :::::" + address.toString());
System.out.println("This is address Bean :::::" + address1.toString());
System.out.println("This is address Bean :::::" + address2.toString());
}
}
Output of the above Application would be
Constructor with Single List argument invoked
Constructor with multiple argument invoked
This is address Bean :::::Address [flatNo=360, bldgNo=C2, streetNo=My Street, city=My City, pincode=276044]
This is address Bean :::::Address [flatNo=203, bldgNo=2C, streetNo=JBT Street, city=New York, pincode=276001]
This is address Bean :::::Address [flatNo=203, bldgNo=2C, streetNo=JBT Street, city=New York, pincode=276001]
Environment Used
Tool : Eclipse Indigo
Java : JDK 1.6
Spring : 3.1.1
Jars Required :
- org.springframework.core-3.1.1.RELEASE.jar
- org.springframework.context-3.1.1.RELEASE.jar
- org.springframework.asm-3.1.1.RELEASE.jar
- org.springframework.beans-3.1.1.RELEASE.jar
- org.springframework.expression-3.1.1.RELEASE.jar
- commong-logging-1.1.1.jar