Javatpoint Logo
Javatpoint Logo

Setter Injection with Dependent Object Example

Like Constructor Injection, we can inject the dependency of another bean using setters. In such case, we use property element. Here, our scenario is Employee HAS-A Address. The Address class object will be termed as the dependent object. Let's see the Address class first:

This class contains four properties, setters and getters and toString() method.

It contains three properties id, name and address(dependent object) , setters and getters with displayInfo() method.


The ref attribute of property elements is used to define the reference of another bean.

This class gets the bean from the applicationContext.xml file and calls the displayInfo() method.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA