How is constructor chosen in spring for beans defined in xml with parent?

心不动则不痛 提交于 2019-12-02 09:00:17
Jerome Anthony

From spring documentation,

Constructor argument resolution matching occurs using the argument’s type. If no potential ambiguity exists in the constructor arguments of a bean definition, then the order in which the constructor arguments are defined in a bean definition is the order in which those arguments are supplied to the appropriate constructor when the bean is being instantiated.

I found the following answer which explains the spring behavior in selecting a constructor.

If you specify a constructor-arg without index, the greediest constructor that can be satisfied with the given arguments will be invoked (matching the arguments by type). In the case of java.io.File, this is the File(String parent, String child) constructor: Your String argument matches both by type, so the algorithm uses that constructor.

reference 1 reference 2

When inheriting from a parent, the constructor arguments get merged (same as merging property collections). In your case, after the merging the child bean constructor arguments will be

<constructor-arg value="23" type="int"/>
<constructor-arg value="10" type="int"/>
<constructor-arg value="Ringing" />

For ambiguous scenarios, use either the index or the constructor argument name.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!