问题
I'm following Spring Reference Documentation 4.0.0.RELEASE. It says
When a simple type is used, Spring cannot determine the type of the value, and so cannot match by type without help
The scenario is as follows
package examples;
public class ExampleBean {
private int years;
private String ultimateAnswer;
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
Now to resolve the arguments, it is instructed to use type attribute in XML
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>
But when I ignore this instruction, then even without using type attribute or index number or name of argument in XML configuration, the Spring container easily resolves the arguments. Please guide me why is it necessary to use the type attributes unnecessarily.
回答1:
When you ignore the instruction and omit type attributes and index numbers, Spring uses its own rules to decide how to interpret the values. If you don't have multiple constructors that Spring can easily confuse, this can be good enough.
If you have constructors with the same number of arguments with different types then the type attributes would clarify which constructor you intend to call. However, Spring doesn't use the order in which the constructor arguments appear in the XML to figure out which constructor to use, so if you have multiple constructors like
public ExampleBean(int years, String ultimateAnswer) {
...
}
public ExampleBean(String stuff, int someNumber) {
...
}
then Spring doesn't differentiate between these, if you configure it as
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="1"/>
<constructor-arg type="java.lang.String" value="foo"/>
</bean>
then Spring is looking for something that takes a string and an int in either order and it can take either constructor as a match; it depends which it finds first. The index number prevents this from happening, it indicates to Spring what order the arguments should appear in.
When your beans have multiple constructors it would seem like a better idea to describe the constructor arguments explicitly than to hope Spring guesses right.
来源:https://stackoverflow.com/questions/31728258/spring-constructor-argument-ambiguity-with-simple-types