Injection of String and Primitive values using Setter and Constructor Injection

In our previous tutorial we learned how to inject object using spring framework, In this tutorial, we will learn how to inject String and Primitive values using Setter and Constructor Injection.

Setter Injection using String and Primitive values: In setter injection using a String or primitive values, we define a tag <property name=”name of variable” value=”value of the variable”></property> in the XML file and a set method into which we want to inject our dependency.

Let’s implement the same example from our previous post, but this time apart from injecting object, we will also inject contact number and app name as String and Integer also.

phone.java


package com.codegeekslab.device;

public interface Phone {

public void openApp(int number, String app);
 
}

BasicPhone.java


package com.codegeekslab.type;

import com.codegeekslab.device.Phone;

public class BasicPhone implements Phone {

public void openApp(int number, String app) {
System.out.println("calling via " + app + " " + number);
}

}

SmartPhone.java


package com.codegeekslab.type;

import com.codegeekslab.device.Phone;

public class SmartPhone implements Phone {

public void openApp(int number, String app) {
System.out.println("calling via " + app + " " + number);
}

}

CallingApp.java

package com.codegeekslab.app;

import com.codegeekslab.device.Phone;

public class CallingApp {

 private Phone phone;

 private int number;
 
 private String app;

 // for setter injection
 public void setPhone(Phone phone) {
 this.phone = phone;
 }
 public Phone getPhone() {
 return phone;
 }

 public int getNumber() {
 return number;
 }
 public void setNumber(int number) {
 this.number = number;
 }
 public String getApp() {
 return app;
 }
 public void setApp(String app) {
 this.app = app;
 }
 public void makeCall() {
 phone.openApp(number,app);
 }

}

beans.xml

&amp;amp;lt;beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&amp;amp;gt;

 &amp;amp;lt;bean id="BasicPhone" class="com.codegeekslab.type.BasicPhone" /&amp;amp;gt;
 &amp;amp;lt;bean id="SmartPhone" class="com.codegeekslab.type.SmartPhone" /&amp;amp;gt;

 &amp;amp;lt;bean id="CallingApp" class="com.codegeekslab.app.CallingApp"&amp;amp;gt;
 &amp;amp;lt;property name="phone" ref="SmartPhone"&amp;amp;gt;&amp;amp;lt;/property&amp;amp;gt;
 &amp;amp;lt;property name="number" value="12345"&amp;amp;gt;&amp;amp;lt;/property&amp;amp;gt;
 &amp;amp;lt;property name="app" value="whatsapp"&amp;amp;gt;&amp;amp;lt;/property&amp;amp;gt;

 &amp;amp;lt;/bean&amp;amp;gt;
&amp;amp;lt;/beans&amp;amp;gt;

output

Mar 19, 2017 4:49:43 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Mar 19, 2017 4:49:44 PM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Sun Mar 19 16:49:44 IST 2017]; root of context hierarchy
calling via whatsapp 12345

In this example, we are injecting 3 items into all set methods of CallingApp class

  1. Dependent Object using <property name=”phone” ref=”SmartPhone”></property>.
  2. Integer value using <property name=”number” value=”12345″></property>.
  3. String Value using <property name=”app” value=”whatsapp”></property>

phone , number, and app are the variables of CallingApp class and we are setting dependent object phone as SmartPhone, number as an int with value 12345 and app as String with value whatsapp.

Constructor Injection using String and Primitive values: In constructor injection using a String or primitive values, we define a tag <constructor-arg value=”value of the variable”/>  in the XML file and a constructor into which we want to inject our dependency.

Let’s implement the same above example using constructor injection.

All files will remain same except beans.xml and CallingApp.java

CallingApp.java


package com.codegeekslab.app;

import com.codegeekslab.device.Phone;

public class CallingApp {

private Phone phone;

private int number;

private String app;

public CallingApp(Phone phone, int number, String app) {
super();
this.phone = phone;
this.number = number;
this.app = app;
}

// for setter injection
public void setPhone(Phone phone) {
this.phone = phone;
}

public Phone getPhone() {
return phone;
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}

public String getApp() {
return app;
}

public void setApp(String app) {
this.app = app;
}

public void makeCall() {
phone.openApp(number, app);
}

}

beans.xml

&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;

&lt;bean id="BasicPhone" class="com.codegeekslab.type.BasicPhone" /&gt;
&lt;bean id="SmartPhone" class="com.codegeekslab.type.SmartPhone" /&gt;

&lt;bean id="CallingApp" class="com.codegeekslab.app.CallingApp"&gt;
&lt;constructor-arg ref="SmartPhone"/&gt;
&lt;constructor-arg value="12345"/&gt;
&lt;constructor-arg value="whatsapp"/&gt;
&lt;/bean&gt;
&lt;/beans&gt;

output


Mar 24, 2017 10:30:58 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Mar 24, 2017 10:30:58 AM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Fri Mar 24 10:30:58 IST 2017]; root of context hierarchy
calling via whatsapp 12345

In this example, we are injecting 3 items into constructor of CallingApp class

  1. Dependent Object using <constructor-arg ref=”SmartPhone”/>.
  2. Integer value using <constructor-arg value=”12345″/>.
  3. String Value using <constructor-arg value=”whatsapp”/>.

The order of defining tags should be according to the order of variable defined in the constructor else you will get an exception.

public CallingApp(Phone phone, int number, String app) {
super();
this.phone = phone;
this.number = number;
this.app = app;
}

As the phone is defined first so the first tag in beans should be <constructor-arg ref=”SmartPhone”/> and so on.

phone , number, and app are the variables of CallingApp class and we are setting dependent object phone as SmartPhone ,number as an int with value 12345 and app as String with value whatsapp.

So, in this we learned injecting string and primitive values using setter and contructor injection ,In our next tutorial we will learn how to inject collections such as list,set and map using setter and constructor injection