Dependency Injection by Constructor in Spring

A bean may have dependency on primitive values,other beans or collections of the beans these dependencies are statisfied by IOC container at the time of bean creation.Dependency of a bean can be statisfied by following two ways.

  • Dependency Injection
  • Dependency Lookup

Dependency Injection is supported by Spring framework to statisfy the dependency of a bean.
Dependency Injection can be of following two types.

  • Setter Injection
  • Constructor Injection

Setter injection required setter methods to fulfill the dependency. The <property> subelement of <bean> is used for setter injection.Constructor Injection required parametrized constructors in the bean class to fulfill the dependency of bean. The <constructor-arg> subelement of <bean> is used for constructor injection.

XML configuration

Here <constructor-arg value=””> is used to provide the primitive values.Sequence of <constructor-arg value=””> will decide which parametrized constructor will invoke.By default the type of value is String if want to provide different than String then type attribute is used.

Steps required to create bean using Contructor injection

  • Add jar file of spring core module.
  • Define POJO class with default constructor,getter and setter methods.
  • XML configuration is created to provide beans configuaration.
  • Resource object is created to provide configuration of beans to IoC container.
  • A BeanFactory object is obtained and reference of Resource object is provided to it.
  • Now getBean() is used to create object
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="" xmlns:xsi="" xmlns:p="" xsi:schemaLocation="">  
<bean id="customerbean" class="com.startwithjava.beans.Customer">   
<constructor-arg value="Raj"></constructor-arg> 
<constructor-arg value="Delhi"></constructor-arg> 
<constructor-arg value="11"></constructor-arg> 

POJO class

package com.startwithjava.beans;
public class Customer {
	private String name;
	private String address;
	private String zipcode;
	public Customer() {
	public Customer(String name, String address, String zipcode) {
		super(); = name;
		this.address = address;
		this.zipcode = zipcode;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getAddress() {
		return address;
	public void setAddress(String address) {
		this.address = address;
	public String getZipcode() {
		return zipcode;
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	public void display(){
		 System.out.println("Name:-"+name+"\nAddress:-"+address+"\nZip Code:-"+zipcode);  

package com.startwithjava;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import com.startwithjava.beans.Customer;
public class TestIOC {

	  public static void main(String[] args) {  
		  Resource resource=new ClassPathResource("applicationContext.xml");  
		    BeanFactory factory=new XmlBeanFactory(resource);  
		    Customer c=(Customer)factory.getBean("customerbean");  

Download Source Code

Leave a Reply