Spring Setter Dependency Injection(No Arg Constructor)

In Setter based DI(Dependency Injection), Objects define their dependencies via properties( 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() {
        System.out.println("Invoking No Arg Constructor");
    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).

      " rel="nofollow" target="_blank">http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


            JBT Street

            New York




¬†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(
        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.

asked Sep 1, 2015 in spring framework by rajesh
0 votes

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.