Spring中构造函数注入类型的歧义

在Spring框架中,当您的类包含多个带有相同数量参数的构造函数时,它将始终导致构造函数注入参数类型含糊不清

问题

让我们看一下这个客户Bean示例。 它包含两个构造函数方法,两个方法都接受3个具有不同数据类型的参数。

package com.mkyong.common;

public class Customer 
{
	private String name;
	private String address;
	private int age;
	
	public Customer(String name, String address, int age) {
		this.name = name;
		this.address = address;
		this.age = age;
	}
	
	public Customer(String name, int age, String address) {
		this.name = name;
		this.age = age;
		this.address = address;
	}
	//getter and setter methods
	public String toString(){
		return " name : " +name + "\n address : "
               + address + "\n age : " + age;
	}

}

在Spring bean配置文件中,为名称传递“ mkyong”,为地址传递“ 188”,并为年龄传递“ 28”。

<!--Spring-Customer.xml-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="CustomerBean" class="com.mkyong.common.Customer">

		<constructor-arg>
			<value>mkyong</value>
		</constructor-arg>
		
		<constructor-arg>
			<value>188</value>
		</constructor-arg>
		
		<constructor-arg>
			<value>28</value>
		</constructor-arg>
        </bean>

</beans>

运行它,您的预期结果是什么?

package com.mkyong.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext context = 
    	  new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});

    	Customer cust = (Customer)context.getBean("CustomerBean");
    	System.out.println(cust);
    }
}

输出量

name : mkyong
 address : 28
 age : 188

结果不是我们预期的,而是运行第二个构造函数,而不是第一个构造函数。 在Spring中,参数类型'188'可以转换为int,因此Spring只是将其转换为第二个构造函数,即使您假定它应该是String。

此外,如果Spring无法解析要使用的构造函数,则会提示以下错误消息

constructor arguments specified but no matching constructor 
found in bean 'CustomerBean' (hint: specify index and/or 
type arguments for simple parameters to avoid type ambiguities)

要修复它,您应该始终通过类似这样的type属性为构造函数指定确切的数据类型:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="CustomerBean" class="com.mkyong.common.Customer">
	
		<constructor-arg type="java.lang.String">
			<value>mkyong</value>
		</constructor-arg>
		
		<constructor-arg type="java.lang.String">
			<value>188</value>
		</constructor-arg>
		
		<constructor-arg type="int">
			<value>28</value>
		</constructor-arg>
		
	</bean>

</beans>

再次运行它,现在您将获得预期的结果。

输出量
name : mkyong
 address : 188
 age : 28

注意
显式声明每个构造函数参数的数据类型始终是一个好习惯,以避免上面出现构造函数注入类型歧义。

翻译自: https://mkyong.com/spring/constructor-injection-type-ambiguities-in-spring/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值