当存在多个构造函数时,使用autowire =“constructor”进行dependency injection?

我有下面的构造函数的文本编辑器类

public class TextEditor { private SpellChecker spellChecker; private SpellChecker1 spellChecker1; private SpellChecker2 spellChecker2; public TextEditor(SpellChecker spellChecker) { this.spellChecker = spellChecker; } public TextEditor(SpellChecker2 spellChecker2) { this.spellChecker2 = spellChecker2; } public TextEditor(SpellChecker spellChecker, SpellChecker1 spellChecker1,SpellChecker2 spellChecker2) { this.spellChecker = spellChecker; this.spellChecker1 = spellChecker1; this.spellChecker2 = spellChecker2; } public TextEditor(SpellChecker spellChecker, SpellChecker1 spellChecker1) { this.spellChecker = spellChecker; this.spellChecker1 = spellChecker1; } } 

在spring豆我有

   

我观察到的是具有两个参数的构造函数被一致地调用。 它是随机的吗? 不应该抛出exceptionbecoz它不知道需要调用哪个构造函数?

这是Spring自动构建器的结果。

它做的第一件事就是获取所有bean类的构造函数并对它们进行排序,首先将公共构造函数放入减少的参数数量,然后再将所有非公共构造函数放入参数数量减少的情况。 这些是候选构造函数。

然后迭代遍历这些候选者,尝试从BeanFactory生成参数。 如果它不能因为bean丢失或出于其他原因,它会跳过候选者。 如果它成功找到参数,它会根据许多因素(参数列表长度,参数类型与参数的接近程度等等)为当前候选构造函数提供权重。 然后它检查前一个候选人的体重,如果一个人比另一个人好,则交换他们。

如果在此过程结束时有候选构造函数,Spring将使用它。

如果你说Spring在你的3 arg构造函数中使用你的2 arg构造函数,那么这意味着你的3 arg构造函数中没有一个类型的bean。

更准确地说,下面的例子可以帮助你理解,所以我有一个类Employee,它与资格和地址有关系,bean在xml声明中可用。

          

我们有4个构造函数,第1个接受两个参数,即Address和Qualification,第二个接受Qualification,第三个构造函数接受Address,然后第4个接受从id,name,dob,address和qualified开始的所有字段。

//构造函数 – 1

 public Employee(Address address, Qualification qualification) { super(); System.out.println(1); this.address = address; this.qualification = qualification; } 

//构造函数 – 2

 public Employee(Qualification qualification) { super(); System.out.println(2); this.qualification = qualification; } 

//构造函数 – 3

 public Employee(Address address) { super(); System.out.println(3); this.address = address; } 

//构造函数 – 4

 public Employee(int id, String name, Date dob, Address address, Qualification qualification) { super(); System.out.println(4); this.id = id; this.name = name; this.dob = dob; this.address = address; this.qualification = qualification; } 

情况1 :假设未声明构造函数1和4,并且我们在employee bean声明中没有构造函数arg。

行为 :构造函数3将被调用,因为这是最后一个构造函数声明的,如果我们改变构造函数定义的顺序,即将构造函数2的位置与3交换。

情况2 :假设未声明构造函数4,并且我们在employee bean声明中没有构造函数arg。

行为 :在这种情况下,将调用构造函数1,因为它可以获得bean声明中可用的类型Qualification和Address,因此这满足构造函数1的匹配参数的条件。

情况3 :假设我们在employee bean声明中没有构造函数arg。

行为 :在这种情况下,也会调用构造函数1,因为它可以获得bean声明中可用的类型Qualification和Address,因此这满足构造函数1的匹配参数的条件,但它不能调用第4个构造函数由于bean声明文件中没有id,name和dob,因此第一个构造函数是最佳匹配构造函数,因为我们在bean声明中提供了Qualification和Address。

案例4 :假设我们在employee bean声明中有构造函数arg,并且所有构造函数都可用。

行为 :它将能够调用第4个构造函数,因为id,name,dob,qualified和address在bean声明文件中可用,因此1st 3参数将来自构造函数arg,而后两个将来自bean本身的声明因此,将调用第4个构造函数匹配的第4个构造函数的所有参数。 结论:所以案例和行为表明,在多个构造函数的情况下,spring容器尝试validation所有依赖属性并基于构造函数中可用于创建对象的所有可用属性进行validation,其中最大可能属性可以被初始化。