这是代码:春重载构造函数注射
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) –