关于Spring构造函数注入的歧义 [英] Ambiguity Regarding Spring Constructor Injection

查看:57
本文介绍了关于Spring构造函数注入的歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我指定了类型,为什么我仍然可以看到第一个构造函数,为什么有人可以向我解释一下幕后发生的事情...因为我不想指定索引位置,因此我需要根据以下内容来调用第二个构造函数类型.

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 ");
    }
}

我的My Beans.xml如下:

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>

推荐答案

始终最好为构造函数args指定确切的数据类型,以避免构造函数注入类型的歧义.有关更多详细信息,请参考示例

It's always better to specify exact data types for constructor args to avoid constructor injection type ambiguities. Refer this example for more details

您可以尝试

<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>

这篇关于关于Spring构造函数注入的歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆