91-9990449935 0120-4256464 |
Setter Injection with Dependent Object ExampleLike 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.javaThis class contains four properties, setters and getters and toString() method. Employee.javaIt contains three properties id, name and address(dependent object) , setters and getters with displayInfo() method. applicationContext.xmlThe ref attribute of property elements is used to define the reference of another bean. Test.javaThis class gets the bean from the applicationContext.xml file and calls the displayInfo() method.
download this example (developed using MyEclipse IDE)
download this example (developed using Eclipse IDE) |