Monday, 25 April 2016

Constructor Injection using Spring Framework

Now that, we already know how to inject the properties of a Bean using <property="   " value=""> its time to call the constructor and inject the values to constructor argument using spring framework. 

Construtor argument injection for single parameter : 

Consider a simple bean as follows : 

class Employee
{
   int empId;
   Employee(int empId)
   {
    this.empId=empId;
    }
}

In above class the Employee bean consist of a property empId, which we are initializing by using constructor.Spring uses the constructor injection concept to call the parameterized constructor of Employee through configuration file as follows : 

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

    <bean id="emp1"
          class="Employee">
        <constructor-arg value="101" />
    </bean>

</beans>
 
The above configuration file will call the single argument constructor in Employee bean and inject the value 101 into the constructor !!!! 

But life is not so simple always : 
Consider the following example : 

public class Employee 
{
 private String name;
 private String address;
 private int age;
 
 public Employee(String name, String address, int age) {
  this.name = name;
  this.address = address;
  this.age = age;
 }
 
 public Employee(String name, int age, String address) {
  this.name = name;
  this.age = age;
  this.address = address;
 }
 //getter and setter methods
 public String toString(){
  return " name : " +name + "\n address : "
               + address + "\n age : " + age;
 }

}
The bean configuration for above Bean would be :
<!--Spring-Customer.xml-->
<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">

 <bean id="EmployeeBean" class="Employee">

  <constructor-arg>
   <value>Amit</value>
  </constructor-arg>
  
  <constructor-arg>
   <value>188</value>
  </constructor-arg>
  
  <constructor-arg>
   <value>28</value>
  </constructor-arg>
        </bean>

</beans>
and when we run the application by loading the bean like : 
Employee emp = (Employee)context.getBean("EmployeeBean");
     System.out.println(emp);
we end up with the output :
 name : Amit
 address : 28
 age : 188
What we are expecting above was the first constructor to be called but the second constructor is getting called. In Spring, the argument type ‘188’ is capable convert to int, so Spring just convert it and take the second constructor, even you assume it should be a String.

Solution
To resolve this we need to always make sure that we sepcify the data types for the argument in the configuration file like :
Markup
<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">

 <bean id="EmployeeBean" class="Employee">
 
  <constructor-arg type="java.lang.String">
   <value>Amit</value>
  </constructor-arg>
  
  <constructor-arg type="java.lang.String">
   <value>188</value>
  </constructor-arg>
  
  <constructor-arg type="int">
   <value>28</value>
  </constructor-arg>
  
 </bean>

</beans>
Run it again, now you get what you expected.
Output

name : Amit
address : 188
age : 28

No comments:

Post a Comment

Attend Online Java Certification Training and excel your career

Hello Java Developer,  Are you staying at home and worried about your future? Do not waste the time in worrying. International certifi...