Thursday 2 February 2017

Spring: Constructor based dependency injection

In this post, I am going to explain how to pass arguments to constructor. For example, following is my constructor definition.

         public Employee(int id, String firstName, String lastName){
                 this.id = id;
                 this.firstName = firstName;
                 this.lastName = lastName;
         }

By using ‘constructor-arg’ tag, you can pass arguments to the constructor.
There are three ways to pass values to constructor.
a.   Passing the arguments in the same way they defined in the constructor.
b.   Passing the arguments using the index value. In the above case, argument id has index 0, firstName has index 1, lastName has index 2.
c.    Passing the arguments using the name of the argument.

a. Passing the arguments in the same order they defined in the constructor.
<bean id="529" name="hari" class="com.sample.pojo.Employee">
 <constructor-arg type="int" value="529" />
 <constructor-arg type="java.lang.String" value="Hari Krishna" />
 <constructor-arg type="java.lang.String" value="Gurram" />
</bean>


b. Passing the arguments using the index value.
<bean id="530" name="sudhir" class="com.sample.pojo.Employee">
 <constructor-arg index="2" value="Sami" />
 <constructor-arg index="0" value="530" />
 <constructor-arg index="1" value="Sudhir Kumar" />
</bean>

c. Passing the arguments using the name of the argument.
<bean id="531" name="kiran" class="com.sample.pojo.Employee">
 <constructor-arg name="lastName" value="Darsi" />
 <constructor-arg name="id" value="531" />
 <constructor-arg name="firstName" value="Kiran Kumar" />
</bean>


Following is the complete working application.

Employee.java
package com.sample.pojo;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

 public Employee(int id, String firstName, String lastName) {
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
    .append(lastName).append("]");
  return builder.toString();
 }

}

myConfiguration.xml
<?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="529" name="hari" class="com.sample.pojo.Employee">
  <constructor-arg type="int" value="529" />
  <constructor-arg type="java.lang.String" value="Hari Krishna" />
  <constructor-arg type="java.lang.String" value="Gurram" />
 </bean>


 <bean id="530" name="sudhir" class="com.sample.pojo.Employee">
  <constructor-arg index="2" value="Sami" />
  <constructor-arg index="0" value="530" />
  <constructor-arg index="1" value="Sudhir Kumar" />
 </bean>


 <bean id="531" name="kiran" class="com.sample.pojo.Employee">
  <constructor-arg name="lastName" value="Darsi" />
  <constructor-arg name="id" value="531" />
  <constructor-arg name="firstName" value="Kiran Kumar" />
 </bean>

</beans>

HelloWorld.java
package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.Employee;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Employee employee1 = context.getBean("hari", Employee.class);
  Employee employee2 = context.getBean("sudhir", Employee.class);
  Employee employee3 = context.getBean("kiran", Employee.class);

  System.out.println(employee1);
  System.out.println(employee2);
  System.out.println(employee3);

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Run HelloWorld.java, you can able to see following output.
Employee [id=529, firstName=Hari Krishna, lastName=Gurram]
Employee [id=530, firstName=Sudhir Kumar, lastName=Sami]
Employee [id=531, firstName=Kiran Kumar, lastName=Darsi]




Previous                                                 Next                                                 Home

No comments:

Post a Comment