Spring Dependency Injection By Constructor

One method to achieve Dependency Injection of a resource is by constructor. The of the element is used for constructor injection. The behaves the exact same way as if you called the constructor.

Injecting Primitive Int & String Values

In this sample, we are going to inject a primitive int as well as a String value into the constructor of our Widget class.

For this Spring tutorial, we will need to create some files:

  • Widget.java
  • WidgetContext.xml
  • WidgetTest.java


Injecting Primitive Int

Widget.java

Our simple Widget Class contains two fields: size and name. Our class contains four constructors and one method.

package com.jcd.spring.tutorials;

public class Widget {
	
	private int size;
	private String name;
	
    //Empty Constructor
    public Widget() { 
         System.out.println("Widget Default Constructor");
    }

    //Constructor for widget size
    public Widget(int size) { 
         this.size = size;
    }
 
    //Constructor for widget name
    public Widget(String name) { 
         this.name = name;
    }

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

    public void displayWidget() {
    	System.out.println("Widget:"+name+" - "+size);
    }
}


WidgetContext.xml

The WidgetContext.xml file will provide information into our bean. The constructor-arg element invokes one of the constructors in our Widget Class. In the sample below, we are essentially calling the second constructor. The value is 500 and we set the type to an int.

<?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="widget" class="com.jcd.spring.tutorials.Widget">  
    <constructor-arg value="500" type="int"></constructor-arg>  
    </bean>  
      
</beans>  


WidgetTest.java

Our WidgetTest Class will retrieve the bean from the WidgetContext.xml file and then call the displayWidget() method.

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


Injecting String

Couple of things to keep in mind. If you don’t specify the type attribute for the constructor-arg element, Spring will default the type to a String. We already injected an int (500) to the constructor. Now let’s inject both and int and a String using two constructor-arg elements.

<?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="widget" class="com.jcd.spring.tutorials.Widget">  
    <constructor-arg value="500" type="int"></constructor-arg>  
    <constructor-arg value="BigWidget"></constructor-arg>
    </bean>  
      
</beans>  


References:

Add a Comment

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