Setter based dependency injection

Setter based dependency injection is a process of passing the dependency to a dependent object via a setter method.
Note:
1. For primitive data types use element and for dependent objects use


2. Index attribute is used to specify the index of constructor arguments.

Example Explanation:

We have created two beans “Student” and “Address”. Student class requires an Address class object. In spring configuration file we define Address bean and pass this as an argument in Student class using parameter element.

Example:

Student.java

package com.w3schools360.business;

/**
 * This class will be used as a bean class.
 * @author W3schools360
 */
public class Student {
	private String name;
	private String rollNo;
	private String className;
	private Address address;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getRollNo() {
		return rollNo;
	}
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}		
}

Address.java

package com.w3schools360.business;

/**
 * This class will be used as a bean class.
 * @author W3schools360
 */
public class Address {
	private String addLine;
	private String city;
	private String state;
	private String country;
	
	public String getAddLine() {
		return addLine;
	}
	public void setAddLine(String addLine) {
		this.addLine = addLine;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}	
}

applicationContext.java




   
   	   
       
       
       
   
   
    
       
       
       
       
    


Test.java

package com.w3schools360.business;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
 public static void main(String[] args) {
  //Get ApplicationContext using spring configuration file.
  ApplicationContext context = 
	new ClassPathXmlApplicationContext("applicationContext.xml");
		
  //Get Student bean object from ApplicationContext instance. 
  Student student = (Student) context.getBean("student");
		
  //Process Student Object.
  System.out.println("Student info: ");
  System.out.println("Name: " + student.getName());
  System.out.println("RollNo: " + student.getRollNo());
  System.out.println("Class: " + student.getClassName());
		
  //Get Address from Student Object.
  Address studentAddress = student.getAddress();
		
  //Process Address Object.
  System.out.println("Student Address: ");
  System.out.println("Address Line: " + studentAddress.getAddLine());
  System.out.println("City: " + studentAddress.getCity());
  System.out.println("State: " + studentAddress.getState());
  System.out.println("Country: " + studentAddress.getCountry());
 } 
}

Output:

Student info: 
Name: Jai
RollNo: MCA/07/06
Class: MCA
Student Address: 
Address Line: Test address
City: Gurgaon
State: Haryana
Country: India

 
Download this example.