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="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
<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> 
</bean> 
</beans> 

POJO class

package com.startwithjava.beans;
public class Customer {
	private String name;
	private String address;
	private String zipcode;
	
	public Customer() {
		super();
	}
	public Customer(String name, String address, String zipcode) {
		super();
		this.name = name;
		this.address = address;
		this.zipcode = zipcode;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.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);  
	}
}

TestIOC.java

package com.startwithjava;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
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");  
		    c.display();  
	          
	    }  
}

Download Source Code

Leave a Reply