Spring Setter Dependency Injection(No Arg Constructor)

In Setter based DI(Dependency Injection), Objects define their dependencies via properties(<property> tag) and Container invoke setter methods to set these properties in object instance after invoking a no-argument constructor or no-argument static factory method to instantiate the bean.

To make setter injection work No-Argument Constructor(Default/Explicit) or No-Argument Static factory method is required. In this topic we will discuss about the DI using no arg constructor. Click here to learn Spring DI using Factory Method.

Bean Class(To  Dependency Inject)

package com.jbt.bean;

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;
	}

	/*
	 * No-Argument Constructor or No-Argument Static factory method is required
	 * to make setter injection work. In case none of them exist(No arg
	 * Constructor/Static method) "No default constructor found;" error will be
	 * thrown
	 */
	private Address() {
		super();
		System.out.println("Invoking No Arg Constructor");
	}

	@Override
	public String toString() {
		return "Address [flatNo=" + flatNo + ", bldgNo=" + bldgNo
				+ ", streetNo=" + streetNo + ", city=" + city + ", pincode="
				+ pincode + "]";
	}

}

 Here we have overriden “toString” method to view meaningfull output for the programmme.

Spring Configuration File

Above bean will be configure in Spring configuring file where we can provide the value to dependency inject in Bean via property tag(Used for setter Injection).

<?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">
	<!-- Here we are using setter injection for DI -->
	<!-- Name Attribute can be used to create one or more aliases for a Bean.
		Multiple aliases can be separated by a spaces, commas, or semi-colons -->
	<bean id="address" class="com.jbt.bean.Address" name="address1,address2">
		<property name="flatNo">
			<value>203</value>
		</property>
		<property name="bldgNo">
			<value>2C</value>
		</property>
		<property name="streetNo">
			<value>JBT Street</value>
		</property>
		<property name="city">
			<value>New York</value>
		</property>
		<property name="pincode">
			<value>123456</value>
		</property>
	</bean>

</beans>

 In Configuration file we use “class” attribute to provide the fully qualified name of the bean’s class.

And “name” attribute is used to create aliases(address1 & address2)

 

 Execute Application(Dependency Inject Bean)

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 injection.
 */
public class SpringDISetter {
	public static void main(String[] args) {

		ApplicationContext cpxac = new ClassPathXmlApplicationContext(
				"applicationContext_DISetter.xml");

		Address address = (Address) cpxac.getBean("address");

		System.out.println("This is address Bean :::::" + address.toString());
		/*
		 * 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 1:::::" + address1.toString());
		System.out.println("This is address Bean 2:::::" + address2.toString());

	}
}

 

Output of the above programme

Invoking No Arg Constructor
Setter method is called
This is address Bean :::::Address [flatNo=203, bldgNo=2C, streetNo=JBT Street, city=New York, pincode=123456]
This is address Bean 1:::::Address [flatNo=203, bldgNo=2C, streetNo=JBT Street, city=New York, pincode=123456]
This is address Bean 2:::::Address [flatNo=203, bldgNo=2C, streetNo=JBT Street, city=New York, pincode=123456]

As you can see that No Argument constructor has been invoked first and the setter method to set the values in it.

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

Leave 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.