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:

Address.java

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

Employee.java

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

applicationContext.xml

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

Test.java

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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA