Dependency Injection is the main functionality provided by Spring IOC(Inversion of Control). The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods.
In Setter Dependency Injection(SDI) the dependency will be injected with the help of setters and getters methods. A bean-configuration file is used to set DI as SDI in the bean. For this, the property to be set with the SDI is declared under the <property> tag in the bean-config file.
Setter Injection with Dependent Object
If there exists a relationship between the classes of our spring application, then we create the instance of the dependent object also called the contained object. After creating the instance of the dependent object we pass it as an argument of the main class container. Example: If an employee has an address, then the instance of the Address class is the dependent object and will be contained in the Employee class.
Implementation: The following example demonstrates a setter injection with a dependent object.
A. Employee.java
Each employee has the following properties:
- Name
- Employee ID
- Department
- Address(Dependent object)
Example: Employee Class
Java
package com.geeksforgeeks.org;
class Employee {
private String name;
private String employeeID;
private String department;
private Address address;
public void setName(String name) { this .name = name; }
public void setemployeeID(String employeeID)
{
this .employeeID = employeeID;
}
public void setdepartment(String department)
{
this .department = department;
}
public void setAddress(Address address)
{
this .address = address;
}
public String getName() { return name; }
public String getemployeeID() { return employeeID; }
public String getdepartment() { return department; }
public Address getAddress() { return address; }
public void display()
{
System.out.println( "Name: " + getName());
System.out.println( "Employee ID: "
+ getEmployeeID());
System.out.println( "Department: "
+ getDepartment());
System.out.println( "Address: "
+ getAddress().toString());
}
}
|
B. Address.java
Address has the following properties:
- House Number
- Pincode
- State
- Country
Example:
Java
package com.geeksforgeeks.org;
class Address {
private String houseNo;
private String pincode;
private String state;
private String country;
public Address(String houseNo, String pincode, String state, String country) {
super ();
this .houseNo = houseNo;
this .pincode = pincode;
this .state = state;
this .country = country;
}
public String toString() {
return "[" + houseNo + "," + pincode + "," + state + "," + country + "]" ;
}
}
|
C. applicationContext.xml
We will use ref attribute of property element for specifying the reference of the address bean in the employee bean.
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans
< bean id = "address" class = "com.geeksforgeeks.org.Address" >
< property name = "houseNo" value = "110/4" ></ property >
< property name = "pincode" value = "121212" ></ property >
< property name = "state" value = "Delhi" ></ property >
< property name = "country" value = "India" ></ property >
</ bean >
< bean id = "employee" class = "com.geeksforgeeks.org.Employee" >
< property name = "name" value = "Ram" ></ property >
< property name = "employeeID" value = "1001" ></ property >
< property name = "department" value = "Software development" ></ property >
< property name = "address" ref = "address" ></ property >
</ bean >
</ beans >
|
D. Application File (Test.java)
This class will call the display() method of Employee class.
Example:
Java
package com.geeksforgeeks.org;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
class Test {
public static void main(String[] args)
{
Resource resource = new ClassPathResource(
"applicationContext.xml" );
BeanFactory factory = new XmlBeanFactory(resource);
Employee e = (Employee)factory.getBean( "employee" );
e.display();
}
}
|
Output:
Name: Ram
Employee ID: 1001
Department: Software development
Address: [110/4, 121212, Delhi, India]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Feb, 2022
Like Article
Save Article