Ambiguity Regarding Spring Constructor Injection

只愿长相守 提交于 2021-01-29 00:10:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!