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.

WidgetDetails.java

package com.jcd.spring.tutorials;

public class WidgetDetails {  

   private String color;  
   private String shape;  

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


Widget.java

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());
    }
}


WidgetContext.xml

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"?>  
<beans  
     xmlns="http://www.springframework.org/schema/beans"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xmlns:p="http://www.springframework.org/schema/p"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

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

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


WidgetTest.java

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");  
    	
    	widget.displayWidget();
    	
    	appContext.close();
          
    }  
}  


References:

Add a Comment

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