Spring(3)-Costructor injection type ambigities in Spring


In Spring frame ewark,when your clast contains multiple constructurs with same number of argments,it will always cause the constructort injection argment type ambiguities.
Problem
Let’s see this customer bean example.It contains two constructor methods,both accept 3 argments with different data type.
package com.mkyong.common;
 
public class Customer 
{
	private String name;
	private String address;
	private int age;
 
	public Customer(String name, String address, int age) {
		this.name = name;
		this.address = address;
		this.age = age;
	}
 
	public Customer(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 + "
address : " + address + "
age : " + age; } }
In Spring bean configration file,pass a‘mkyong’for name’188’for address and’28’for age.
<!--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="CustomerBean" class="com.mkyong.common.Customer">
 
		<constructor-arg>
			<value>mkyong</value>
		</constructor-arg>
 
		<constructor-arg>
			<value>188</value>
		</constructor-arg>
 
		<constructor-arg>
			<value>28</value>
		</constructor-arg>
        </bean>
 
</beans>
Run it、what’s your expected reult?
package com.mkyong.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext context = 
    	  new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
 
    	Customer cust = (Customer)context.getBean("CustomerBean");
    	System.out.println(cust);
    }
}Output

 name : mkyong
 address : 28
 age : 188
The result is not what we expected、the second construct is run、instead of the first construct.In Spring、the argment type'188's capable convert to int、so Spring just convert it and the sed structures.structures.struct.structures.strument.
Intio addition、if Spring can’t resolive which constructor to use、it will prompt following error message constructor argments specified butのmatching contructor found in bean'Custon'があります.you shound always specify the exact data type for constructor、via type atribute like this:
<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="CustomerBean" class="com.mkyong.common.Customer">
 
		<constructor-arg type="java.lang.String">
			<value>mkyong</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:mkyong address:188 age:28
Note:It’s always a good practice to explicitly declead the data type for each constructor argment,to avoid construct injection type ambigities ise above.