Scroll to top

Spring Dependency Inject using Constructor

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) { = 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) {
		System.out.println("Constructor with multiple argument invoked");
		this.flatNo = flatNo;
		this.bldgNo = bldgNo;
		this.streetNo = streetNo; = city;
		this.pincode = pincode;

	Address(List l1) {
		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); = (String) l1.get(3);
		this.pincode = Integer.parseInt((String) l1.get(4));

	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=""

	<!-- This is constructor injection. Constructor with List argument will
		be invoked -->
	<bean id="addressList" name="address1,address2" class="com.jbt.bean.Address">
				<value>JBT Street</value>
				<value>New York</value>

	<!-- 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"
		<constructor-arg type="java.lang.String" value="C2"


Execute the Programme

package com.jbt;

import org.springframework.context.ApplicationContext;

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(

		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
Author avatar

Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.