问题
why am i able to see First Constructor even if i specified the type can anyone please explain me what is happening behind the scene... since i don't want to specify the index positions i need to call second constructor based on the type.
public class Employee {
String name;
int id;
public Employee(String name,int id) {
System.out.println("First Constrcuot ");
}
public Employee(int id,String name){
System.out.println("Second Constrcuot ");
}
}
I have My Beans.xml as follows:
<bean id="employee" class="com.test.di.Employee">
<constructor-arg type="int">
<value>10</value>
</constructor-arg>
<constructor-arg>
<value>100</value>
</constructor-arg>
</bean>
回答1:
It's always better to specify exact data types for constructor args to avoid constructor injection type ambiguities. Refer this example for more details
Can you try
<bean id="employee" class="com.test.di.Employee">
<constructor-arg type="int">
<value>10</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>100</value>
</constructor-arg>
</bean>
来源:https://stackoverflow.com/questions/30877399/ambiguity-regarding-spring-constructor-injection