Spring Setter Injection in the form of object

We knew how spring bean class object depends on the string primitive, now we will see how our class is depends upon other class in the form of object. When one spring bean class is depend on another spring bean class for performing some logic, this process of dependency is called object dependency. If object dependency is there then in spring framework, the spring IOC container is responsible for creating that required object and injecting into the dependent class.


Let's see an example for better clarity:

Required Files:

  1. Address.java
  2. Employee.java
  3. spconfig2.xml
  4. SpringCoreExample2.java


package java8s;

public class Address {
	private String city,state,country;

	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;
	public String toString()
		return city+" "+state+" "+country;




package java8s;
public class Employee {
private int id;
private String name;
private Address add;
public int getId() {
return id;
public void setId(int id) {
this.id = id;
public String getName() {
return name;
public void setName(String name) {

this.name = name;
public Address getAdd() {
return add;
public void setAdd(Address add) {
this.add = add;
public void show()
System.out.println(id+" "+name);




<?xml version="1.0" encoding="UTF-8"?>
               <bean id="id1" class="java8s.Address">  
               <property name="city" value="BBSR"></property>  
               <property name="state" value="Odisha"></property>
               <property name="country" value="India"></property>
               <bean id="id2" class="java8s.Employee">  
               <property name="id" value="101"></property>  
               <property name="name" value="Akshay"></property>
               <property name="add" ref="id1"></property>



package java8s;

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;

public class SpringCoreExample2 {

	public static void main(String[] args) {
		Resource res=new ClassPathResource("spconfig2.xml");
		BeanFactory bf=new XmlBeanFactory(res);
		Object ob=bf.getBean("id2");
		Employee obj=(Employee)ob;





 101 akshay

 BBSR Odisha India


Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252