trainingtrains Logo

91-9990449935

 0120-4256464

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.