Rectangle(int x, String y){
this.x=x;
this.y=y;
}
Rectangle(String y, int z){
this.z=z;
this.y=y;
}
在上面的代码中,我在 xml 中使用了以下内容:-
<constructor-arg type="int" value="10"/>
<constructor-arg type="java.lang.String" value="10"/>
在这种情况下工作的构造函数是第二个……为什么? spring 如何在这里决定调用哪一个
最佳答案
基本上发生这种情况是因为在调用构造函数时不会考虑参数在 bean 配置文件中出现的顺序。
要解决这个问题,您可以使用索引属性来指定构造函数参数索引。下面是添加index属性后的bean配置文件:
<bean id="rectangle" class="com.shape.rectangle" >
<constructor-arg index="0" type="int" value="10"/>
<constructor-arg index="1" type="java.lang.String" value="10"/>
</bean>
关于java - spring 的构造函数参数行为... 为什么要遵循以下行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31359795/