91-9990449935 0120-4256464 |
Constructor Injection with Dependent ObjectIf there is HAS-A relationship between the classes, we create the instance of dependent object (contained object) first then pass it as an argument of the main class constructor. 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 three properties, one constructor and toString() method to return the values of these object. Employee.javaIt contains three properties id, name and address(dependent object) ,two constructors and show() method to show the records of the current object including the depedent object. applicationContext.xmlThe ref attribute is used to define the reference of another object, such way we are passing the dependent object as an constructor argument. Test.javaThis class gets the bean from the applicationContext.xml file and calls the show method.
download this example (developed using MyEclipse IDE)
download this example (developed using Eclipse IDE) |