tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > Core > Constructor Argument Name Resolution

Constructor Argument Name Resolution 

Spring Inversion of Control (IoC) also known as Dependency Injection (DI) is a process by which objects define their dependencies with collaborating objects. Spring uses the arguments defined in the bean definition in the same order while invoking constructor as long as there is no ambiguity. For instance string "1234" might require to be injected as a either string or number depending on the context. Spring cannot resolve such ambiguity by itself. In such cases we can resolve the ambiguity by explicitly specifying argument name in bean definition. We have to use @ConstructorProperties annotation to specify constructor argument names in class definition.

File Name  :  
/SpringCore001/conf/cons/con_arg_name_props.xml 

File Name  :  
com/bethecoder/tutorials/spring3/basic/Employee2.java 
   
package com.bethecoder.tutorials.spring3.basic;

import java.beans.ConstructorProperties;

public class Employee2 {
  
  private int id;
  private String name;
  private double salary;
  
  
  public Employee2() {
    super();
    this.id = -1;
    this.name = "Unknown";
    this.salary = 0;
  }
  
  @ConstructorProperties(value = "id""name" })
  public Employee2(int id, String name) {
    super();
    this.id = id;
    this.name = name;
    this.salary = 20000;
  }
  
  @ConstructorProperties(value = "id""name""salary" })
  public Employee2(int id, String name, double salary) {
    super();
    this.id = id;
    this.name = name;
    this.salary = salary;
  }

  public int getId() {
    return id;
  }
  
  public void setId(int id) {
    this.id = id;
  }
  
  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public double getSalary() {
    return salary;
  }
  
  public void setSalary(double salary) {
    this.salary = salary;
  }
  
  public String toString() {
    return "Employee[" + id + ", " + name + ", " + salary + "]";
  }
  
}
   

File Name  :  
com/bethecoder/tutorials/spring3/tests/cons/ConsArgNameWithConProps.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests.cons;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.bethecoder.tutorials.spring3.basic.Employee2;

public class ConsArgNameWithConProps {

  /**
   @param args
   */
  public static void main(String[] args) {
    XmlBeanFactory factory = new XmlBeanFactory(
                new ClassPathResource("con_arg_name_props.xml"));

    //Constructor argument index
    Employee2 emp1 = (Employee2factory.getBean("emp1");
    System.out.println(emp1);
    
    Employee2 emp2 = (Employee2factory.getBean("emp2");
    System.out.println(emp2);
  }

}
   

It gives the following output,
Employee[4, PQR, 20000.0]
Employee[5, MNO, 60000.9]



 
  


  
bl  br