在Spring框架中,当一个类包含多个构造函数带的参数相同,它总是会造成构造函数注入参数类型歧义的问题。

问题

让我们来看看这个客户 bean 实例。它包含两个构造方法,均接受3个不同的数据类型参数。
package com.yiibai.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 的配置文件中,通过传递一个“yiibai' 的名字,地址为'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.yiibai.common.Customer"> <constructor-arg>
<value>yiibai</value>
</constructor-arg> <constructor-arg>
<value>188</value>
</constructor-arg> <constructor-arg>
<value>28</value>
</constructor-arg>
</bean> </beans>
运行它,你期望的结果是什么?
package com.yiibai.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 : yiibai
address : 28
age : 188
其结果不是我们所期望的,第一个构造器不执行,而是第二构造函数运行。在Spring参数类型'188' 能够转换成int,所以Spring只是转换它,并采用第二个构造来执行,即使你认为它应该是一个字符串。
另外,如果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)

解决

为了解决这个问题,应该为构造函数指定的确切数据类型,通过像这样类型的属性:
<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.yiibai.common.Customer"> <constructor-arg type="java.lang.String">
<value>yiibai</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 : yiibai
address : 188
age : 28

这是一个很好的做法,显式声明每个构造函数参数的数据类型,以避免上述构造注入型歧义的问题。
 
http://www.yiibai.com/spring/constructor-injection-type-ambiguities-in-spring.html

Spring构造方法注入类型歧义的更多相关文章

  1. spring之注入类型

    spring有三种注入类型: set注入: 构造注入: 接口注入: 一.set注入(引用spring官方文档中的例子)(用的最多) 1.首先在代码中我们需要编写成员变量的set方法,如下所示,一般情况 ...

  2. spring 构造方法注入和setter方法注入的XML表达

    1.构造方法注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC ...

  3. Spring自动注入,类型注入、名称注入(两种方式)

    参考: https://blog.csdn.net/qq_41767337/article/details/89002422 https://www.iteye.com/blog/breezylee- ...

  4. Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配

    1.代码结构图 xxx 2.bean代码 package com.xxx.bean; /** * Created with IntelliJ IDEA. * User: zhenwei.liu * D ...

  5. Spring容器三种注入类型

    Spring注入有三种方式: 1.Set注入(使用最多) 2.构造器注入(使用不多) 3.接口注入(几乎不用)不做测试了 1.Set注入:所谓Set注入就是容器内部调用了bean的Set***方法,注 ...

  6. Spring属性注入、构造方法注入、工厂注入以及注入参数(转)

    Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...

  7. Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件

    1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...

  8. Spring 03: 基于xml的构造方法注入

    构造方法注入 具体有3种注入方式:通过构造方法的 a.参数名称注入 b.参数下标注入 c.默认参数顺序注入 参数名称注入 School实体类 package com.example.pojo03; p ...

  9. Spring通过构造方法注入的四种方式

    通过构造方法注入,就相当于给构造方法的参数传值 set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选 的,构造注入的优势是通过构造强制依赖关系,不可能实例化不 完全的或无法使用的bean. Me ...

随机推荐

  1. caffe Python API 之SoftmaxWithLoss

    net.loss = caffe.layers.SoftmaxWithLoss(net.fc3, net.label) 输出: layer { name: "loss" type: ...

  2. [转载]理解Tomcat的Classpath-常见问题以及如何解决

    摘自: http://www.linuxidc.com/Linux/2011-08/41684.htm 在很多Apache Tomcat用户论坛,一个问题经常被提出,那就是如何配置Tomcat的cla ...

  3. 出现ERROR: While executing gem ... (Gem::FilePermissionError)这种错误的解决办法

    重新安装ruby即可解决 brew install ruby

  4. css给奇数行或偶数行添加指定样式

    odd表示奇数行,even表示偶数行; tr:nth-child(odd); .table-striped > tbody > tr:nth-child(odd) { background ...

  5. LeetCode862. Shortest Subarray with Sum at Least K

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

  6. poj 2337(单向欧拉路的判断以及输出)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11648   Accepted: 3036 Descrip ...

  7. csu 1552(米勒拉宾素数测试+二分图匹配)

    1552: Friends Time Limit: 3 Sec  Memory Limit: 256 MBSubmit: 723  Solved: 198[Submit][Status][Web Bo ...

  8. 使用亚马逊云服务器EC2做深度学习(四)配置好的系统镜像

    这是<使用亚马逊云服务器EC2做深度学习>系列的第四篇文章. (一)申请竞价实例  (二)配置Jupyter Notebook服务器  (三)配置TensorFlow  (四)配置好的系统 ...

  9. Ubuntu16.4 修改静态ip地址

    root@temple-102:~# ifconfig eno1 Link encap:Ethernet HWaddr 0c:c4:7a:e6:49:74 UP BROADCAST MULTICAST ...

  10. 趣味js【练习题】

    1.无限极函数递归,使每次的参数相乘 需求:add(1)(2)(3)(4)(5) 1.1首先要知道一个东西,就是function每次调用,都会默认执行tosting 1.2利用递归,每次返回的都是函数 ...