Spring Constructor Injection Dependent Object

When we have a “has a” relationship between classes, we instantiate an instance of the dependent object. Once created, we pass it as an argument of the main class constructor.

In our sample code below. Widget “has a” relationship to our WidgetDetails. The WidgetDetails class object will be playing the role of the dependent object.


package com.jcd.spring.tutorials;

public class WidgetDetails {  

   private String color;  
   private String shape;  

   public WidgetDetails(String color, String shape) {  
       this.color = color;  
       this.shape = shape;  
   public String toString() {  
       return "WidgetDetails:"+color+" - "+shape;  


Our simple Widget Class contains three fields: size, name, and widgetDetails (dependent object). Our class contains a default constructor, constructor with the three values, and a display method.

package com.jcd.spring.tutorials;

public class Widget {
	private int size;
	private String name;
    private WidgetDetails widgetDetails; /*our dependent object */
    //Empty Constructor
    public Widget() { 
         System.out.println("Widget Default Constructor");

    //Constrcutor for size, name, and widgetDetails
    public Widget(int size, String name, WidgetDetails widgetDetails) {	
          this.size = size;
          this.name = name;
          this.widgetdetails = widgetDetails;

    //Display widget and details
    public void displayWidget() {
    	System.out.println("Widget:"+name+" - "+size);
        System.out.println("Widget Details:"+widgetDetails.toString());


We are introducing the “ref” element below. The ref element is used to define a reference to another object. We are passing our new dependent object (WidgetDetails) as an additional constructor argument.

<?xml version="1.0" encoding="UTF-8"?>  

    <bean id="widgetdetails" class="com.jcd.spring.tutorials.WidgetDetails">  
       <constructor-arg value="blue"></constructor-arg>  
       <constructor-arg value="rectangle"></constructor-arg>  

    <bean id="widget" class="com.jcd.spring.tutorials.Widget"> 
       <constructor-arg value="500" type="int"></constructor-arg>  
       <constructor-arg value="RectangleWidget"></constructor-arg>
          <ref bean="widgetdetails"/>  


Our WidgetTest Class will retrieve the bean from the WidgetContext.xml file and then call the displayWidget() method. It’s also good programming practice to call close() method of the application context class.

package com.jcd.spring.tutorials;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class WidgetTest {

    public static void main(String[] args) {  
    	ConfigurableApplicationContext appContext = new ClassPathXmlApplicationContext("WidgetConfig.xml");
    	BeanFactory factory = (BeanFactory) appContext;
    	Widget widget = (Widget) factory.getBean("widget");  


Add a Comment

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