Dependency Injection by setter methodWe can inject the dependency by setter method also. The
Injecting primitive and string-based values by setter methodLet's see the simple example to inject primitive and string-based values by setter method. We have created three files here:
It is a simple class containing three fields id, name and city with its setters and getters and a method to display these informations. We are providing the information into the bean by this file. The property element invokes the setter method. The value subelement of property will assign the specified value. This class gets the bean from the applicationContext.xml file and calls the display method. Output:20 Arun ghaziabad download this example (developed using MyEclipse IDE)
download this example (developed using Eclipse IDE) |