這是代碼:春重載構造函數注射
public class Triangle {
private String color;
private int height;
public Triangle(String color,int height){
this.color = color;
this.height = height;
}
public Triangle(int height ,String color){
this.color = color;
this.height = height;
}
public void draw() {
System.out.println("Triangle is drawn , +
"color:"+color+" ,height:"+height);
}
}
Spring配置文件是:
<bean id="triangle" class="org.tester.Triangle">
<constructor-arg value="20" />
<constructor-arg value="10" />
</bean>
是否有任何具體的規則來確定哪個構造函數將被Spring叫什麼名字?
僅供參考 - 我在刪除我的答案。我誤解了http://static.springsource.org/spring/docs/3.1.3.RELEASE/spring-framework-reference/html/beans.html#beans-constructor-injection上的spring文檔。對我來說,它似乎選擇了類中的第一個構造函數,但我不認爲它是有保證的,而其他人可能會得到不同的結果。 –
我的WAG將是Java在使用反射時返回構造函數的順序。我想它使用第一個修復。如果你想知道真實的答案,請查看來源。 – JustinKSU
參見[Spring constructor argument ambiguity](http://stackoverflow.com/q/31728258/217324) –