Constructor injection type ambiguities in spring tutorial

In case constructor based dependency injection if our class contains multiple constructors with different types and same number of arguments then spring framework cause the constructor injection argument type ambiguities issue. Let us discuss it with below example.

Example Explanation:

We have created one bean class “Student” which have two constructors with same number of arguments but with different data types. First constructor have String name, int rollNo and second constructor have String name, String className arguments. In spring configuration file we pass arguments ‘jai’ and 27. It should call first constructor but it calls the second constructor and the result is not as expected.

Example:

Student.java

package com.javawithease.business;
 
/**
 * This class will be used as a bean class.
 * @author javawithease
 */
public class Student {	
	private String name;
	private int rollNo;
	private String className;
 
	public Student(String name, int rollNo){
		this.name = name;
		this.rollNo = rollNo;
	}
 
	public Student(String name, String className){
		this.name = name;
		this.className = className;
	}
 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}
 
	public String getClassName() {
		return className;
	}
 
	public void setClassName(String className) {
		this.className = className;
	}	
 
}

applicationContext.java

<?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-3.0.xsd">
 
   <bean id="student" class="com.javawithease.business.Student">
       <constructor-arg value="Jai"/>
       <constructor-arg value="27"/>
   </bean>
 
</beans>

Test.java

package com.javawithease.business;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
  public static void main(String[] args) {
	//Get ApplicationContext using spring configuration file.
	ApplicationContext context = 
	   new ClassPathXmlApplicationContext("applicationContext.xml");
 
	//Get Student bean object from ApplicationContext instance. 
	Student student = (Student) context.getBean("student");
 
	//Process Student Object.
	System.out.println("Student info: ");
	System.out.println("Name: " + student.getName());
	System.out.println("RollNo: " + student.getRollNo());
	System.out.println("Class: " + student.getClassName());		
  } 
}

Output:

Student info: 
Name: Jai
RollNo: 0
Class: 27

Download this example.
 

Solution:

We have to specify the constructor argument’s data types using type attribute. Now it will call the first constructor and the result is as expected.

Example:

Student.java

package com.javawithease.business;
 
/**
 * This class will be used as a bean class.
 * @author javawithease
 */
public class Student {	
	private String name;
	private int rollNo;
	private String className;
 
	public Student(String name, int rollNo){
		this.name = name;
		this.rollNo = rollNo;
	}
 
	public Student(String name, String className){
		this.name = name;
		this.className = className;
	}
 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}
 
	public String getClassName() {
		return className;
	}
 
	public void setClassName(String className) {
		this.className = className;
	}	
 
}

applicationContext.java

<?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-3.0.xsd">
 
   <bean id="student" class="com.javawithease.business.Student">
       <constructor-arg  value="Jai"/>
       <constructor-arg value="27" type="int"/>
   </bean>
 
</beans>

Test.java

package com.javawithease.business;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
  public static void main(String[] args) {
	//Get ApplicationContext using spring configuration file.
	ApplicationContext context = 
	   new ClassPathXmlApplicationContext("applicationContext.xml");
 
	//Get Student bean object from ApplicationContext instance. 
	Student student = (Student) context.getBean("student");
 
	//Process Student Object.
	System.out.println("Student info: ");
	System.out.println("Name: " + student.getName());
	System.out.println("RollNo: " + student.getRollNo());
	System.out.println("Class: " + student.getClassName());		
  } 
}

Output:

Student info: 
Name: Jai
RollNo: 27
Class: null

Download this example.
 
In case when we have one constructor with more than one parameter and do not defined the argument type then the spring framework may through org.springframework.beans.factory.UnsatisfiedDependencyException exception.

Example:

Student.java

package com.javawithease.business;
 
/**
 * This class will be used as a bean class.
 * @author javawithease
 */
public class Student {	
	private String name;
	private int rollNo;
 
	public Student(String name, int rollNo){
		this.name = name;
		this.rollNo = rollNo;
	}
 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}	
}

applicationContext.java

<?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-3.0.xsd">
 
   <bean id="student" class="com.javawithease.business.Student">
       <constructor-arg value="27"/>
       <constructor-arg value="jai"/>
   </bean>
 
</beans>

Test.java

package com.javawithease.business;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
  public static void main(String[] args) {
	//Get ApplicationContext using spring configuration file.
	ApplicationContext context = 
	   new ClassPathXmlApplicationContext("applicationContext.xml");
 
	//Get Student bean object from ApplicationContext instance. 
	Student student = (Student) context.getBean("student");
 
	//Process Student Object.
	System.out.println("Student info: ");
	System.out.println("Name: " + student.getName());
	System.out.println("RollNo: " + student.getRollNo());
  } 
}

Output:

Exception in thread "main" 
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'student' defined in class path resource 
[applicationContext.xml]: Unsatisfied dependency expressed through 
constructor argument with index 1 of type [int]: Could not convert 
constructor argument value of type [java.lang.String] to required 
type [int]: Failed to convert value of type 'java.lang.String' to 
required type 'int'; nested exception is java.lang.NumberFormatException:
For input string: "jai"

Download this example.
 

Solution:

We have to use index attribute to specify the index of constructor arguments.

Example:

Student.java

package com.javawithease.business;
 
/**
 * This class will be used as a bean class.
 * @author javawithease
 */
public class Student {	
	private String name;
	private int rollNo;
 
	public Student(String name, int rollNo){
		this.name = name;
		this.rollNo = rollNo;
	}
 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}	
}

applicationContext.java

<?xml version="1.0" encoding="UTF-8"?>
<?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-3.0.xsd">
 
   <bean id="student" class="com.javawithease.business.Student">
       <constructor-arg index="1" value="27"/>
       <constructor-arg index="0" value="jai"/>
   </bean>
 
</beans>

Test.java

package com.javawithease.business;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
  public static void main(String[] args) {
	//Get ApplicationContext using spring configuration file.
	ApplicationContext context = 
	   new ClassPathXmlApplicationContext("applicationContext.xml");
 
	//Get Student bean object from ApplicationContext instance. 
	Student student = (Student) context.getBean("student");
 
	//Process Student Object.
	System.out.println("Student info: ");
	System.out.println("Name: " + student.getName());
	System.out.println("RollNo: " + student.getRollNo());	
  } 
}

Output:

Student info: 
Name: jai
RollNo: 27

Download this example.