Spring 构造函数参数歧义与简单类型 [英] Spring Constructor Argument Ambiguity with Simple Types

查看:24
本文介绍了Spring 构造函数参数歧义与简单类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 Spring 参考文档 4.0.0.RELEASE.它说

I'm following Spring Reference Documentation 4.0.0.RELEASE. It says

使用简单类型时,Spring不能确定值的类型,因此在没有帮助的情况下无法按类型匹配

When a simple type is used, Spring cannot determine the type of the value, and so cannot match by type without help

场景如下

package examples;
public class ExampleBean {
    private int years;
    private String ultimateAnswer;
    public ExampleBean(int years, String ultimateAnswer) {
        this.years = years;
        this.ultimateAnswer = ultimateAnswer;
     }
 }

现在要解析参数,指示在XML

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>

但是当我忽略此指令时,即使在 XML 配置中不使用 type 属性索引号参数名称,Spring容器轻松解决参数.请指导我为什么有必要不必要地使用类型属性.

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.

推荐答案

当您忽略指令并省略类型属性和索引号时,Spring 会使用自己的规则来决定如何解释这些值.如果您没有多个 Spring 容易混淆的构造函数,那么这已经足够了.

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.

如果您的构造函数具有相同数量的不同类型的参数,那么类型属性将阐明您打算调用哪个构造函数.但是,Spring 不使用构造函数参数在 XML 中出现的顺序来确定要使用哪个构造函数,因此如果您有多个构造函数,例如

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) {
    ...
}

那么Spring不区分这些,如果你配置为

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>

然后 Spring 正在寻找以任意顺序接受 string 和 int 的东西,并且它可以将任一构造函数作为匹配项;这取决于它首先找到哪个.索引号可以防止这种情况发生,它向 Spring 表明参数应该出现的顺序.

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.

当您的 bean 有多个构造函数时,显式描述构造函数参数似乎比希望 Spring 猜对更好.

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.

这篇关于Spring 构造函数参数歧义与简单类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆