Constructor Injection with Dependent Object

This article contains detailed description with example on “Constructor Injection with Dependent Object” topic.

Whenever we have HAS-A relationship between the classes, instance of dependent object comes into the picture. We create the instance of dependent object (contained object) first then pass it as an argument of the main class constructor.

In this example we will discuss about constructor injection with dependent object using a simple and self explanatory program.

This class contains three properties, one constructor and toString() method to return the values of these object.

It contains three properties id, name and address(dependent object) ,constructors and show() method to show the records of the current object including the depedent object.


The ref attribute is used to define the reference of another object, such way we are passing the dependent object as an constructor argument.

This class gets the bean from the applicationContext.xml file and calls the show method.


ID:10 Name:Smith City:Pune State:Maharashtra Country:India

Leave a Reply

Your email address will not be published. Required fields are marked *