Constructor injection type ambiguities in Spring
来源:互联网 发布:无线蓝牙耳机推荐 知乎 编辑:程序博客网 时间:2024/05/24 06:46
In Spring framework, when your class contains multiple constructors with same number of arguments, it will always cause the constructor injection argument type ambiguities issue.
Problem
Let’s see this customer bean example. It contains two constructor methods, both accept 3 arguments with different data type.
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; }}
In Spring bean configuration file, pass a ‘mkyong
’ for name, ‘188
’ for address and ’28’
for age.
<!--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/beanshttp://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>
Run it, what’s your expected result?
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); }}
Output
name : mkyong address : 28 age : 188
The result is not what we expected, the second constructor is run, instead of the first constructor. In Spring, the argument type ‘188
’ is capable convert to int
, so Spring just convert it and take the second constructor, even you assume it should be a String
.
In addition, if Spring can’t resolve which constructor to use, it will prompt following error message
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)
Solution
To fix it, you should always specify the exact data type for constructor, via type attribute like this :
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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>
Run it again, now you get what you expected.
Output
name : mkyong address : 188 age : 28
Note
It’s always a good practice to explicitly declared the data type for each constructor argument, to avoid constructor injection type ambiguities issue above.
- Constructor injection type ambiguities in Spring
- Constructor injection type ambiguities in Spring
- Spring(3)-Constructor injection type ambiguities in Spring
- Constructor injection type ambiguities in Spring /使用Spring时,构造函数注入的歧义
- Difference between Setter Injection vs Constructor Injection in Spring framework
- Spring学习笔记(二)----Constructor Injection or Setter Injection
- Spring学习笔记(二)----Constructor Injection or Setter Injection
- SCSF - Part 6 Constructor Injection in the CAB
- Spring v3.0.2 Learning Note 7 - Constructor-based dependency injection
- Spring的注入类型(Injection type of Spring)
- Constructor argument type matching
- hint: specify index/type/name arguments for simple parameters to avoid type ambiguities
- spring&Injection of resource dependencies failed&Bean named 'generalDao' must be of type [com.oracle
- Spring3注入类型(Injection Type)
- Sql injection in DB2
- EJB injection in Servlets
- module injection in . kernel
- Spring启动时报异常:No String constructor found on type [java.lang.reflect.Method]
- navigationItem.rightBarButtonItem 设置背景图片,颜色更改解决办法
- UITextField详解
- ios 应用 跳转App Store 评论 评分
- 5-8 File Transfer (25分) (简单的并查集, 注意getchar())
- php中 自己写的各种实用正则表达式-实时更新 2015-08-21 15:30:43
- Constructor injection type ambiguities in Spring
- 转载:epoll经验谈
- swift string extension
- js new一个函数和直接调用函数的区别
- Android SDK在线更新镜像服务器无需翻墙
- ImageLoader配置详解
- 严重: Exception loading sessions from persistent storage
- 线程操作:冻结与解冻线程
- UVA 10082 WERTYU