Spring构造方法注入类型歧义
问题
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-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
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
Spring构造方法注入类型歧义的更多相关文章
- spring之注入类型
spring有三种注入类型: set注入: 构造注入: 接口注入: 一.set注入(引用spring官方文档中的例子)(用的最多) 1.首先在代码中我们需要编写成员变量的set方法,如下所示,一般情况 ...
- spring 构造方法注入和setter方法注入的XML表达
1.构造方法注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC ...
- Spring自动注入,类型注入、名称注入(两种方式)
参考: https://blog.csdn.net/qq_41767337/article/details/89002422 https://www.iteye.com/blog/breezylee- ...
- Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配
1.代码结构图 xxx 2.bean代码 package com.xxx.bean; /** * Created with IntelliJ IDEA. * User: zhenwei.liu * D ...
- Spring容器三种注入类型
Spring注入有三种方式: 1.Set注入(使用最多) 2.构造器注入(使用不多) 3.接口注入(几乎不用)不做测试了 1.Set注入:所谓Set注入就是容器内部调用了bean的Set***方法,注 ...
- Spring属性注入、构造方法注入、工厂注入以及注入参数(转)
Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...
- Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件
1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...
- Spring 03: 基于xml的构造方法注入
构造方法注入 具体有3种注入方式:通过构造方法的 a.参数名称注入 b.参数下标注入 c.默认参数顺序注入 参数名称注入 School实体类 package com.example.pojo03; p ...
- Spring通过构造方法注入的四种方式
通过构造方法注入,就相当于给构造方法的参数传值 set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选 的,构造注入的优势是通过构造强制依赖关系,不可能实例化不 完全的或无法使用的bean. Me ...
随机推荐
- selenium启动chrome模拟器模拟手机
一.如果chrome选项里边有这个模拟设备(比如iPhone 6 Plus): 1.先启动Selenium Grid, 比如命令:java -jar selenium-server-standalon ...
- #include<stdarg.h> 可变参数使用
今天上计算方法这课时觉得无聊至极,于是拿出C++编程之道来看了看..无意之中看到了#include<stdarg.h> va_list,va_start,va_end等东西,不知是怎么用的 ...
- EF – 2.EF数据查询基础(上)查询数据的实用编程技巧
目录 5.4.1 查询符合条件的单条记录 EF使用SingleOrDefault()和Find()两个方法查询符合条件的单条记录. 5.4.2 Entity Framework中的内部数据缓存 DbS ...
- PHP完整的AES加解密算法使用及例子(256位)
依赖PHP自身的mcrypt扩展 <?php class aes { // CRYPTO_CIPHER_BLOCK_SIZE 32 private $_secret_key = 'default ...
- Maven使用第三方jar文件的两种方法<转>
http://www.cnblogs.com/sekai/p/5932206.html 今天用上了.. ===================== 在Maven中,使用第三方库一般是通过pom.xml ...
- day4迭代器&生成器&正则表达式
一.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不能后退,不过这也没什么,因为人们很少在迭代途中后退.另外,迭代器的一大优点 ...
- java 获取路径的各种方法
(1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getRequestURI ...
- openssl 获取证书中的公钥
PEM 格式 1. FILE *fp = fopen("xx.pem", "r"); 2. X509 *cert = PEM_read_X509(fp, N ...
- linux 把ls -R格式化成树状结构
谁能写脚本把linux中的ls -R命令的结果格式化成树状结构? 最好是shell脚本!欢迎讨论! 参与讨论有可能意外获取iPhone6哦~~
- 洛谷P3758/BZOJ4887 [TJOI2017] 可乐 [矩阵快速幂]
洛谷传送门,BZOJ传送门 可乐 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 299 Solved: 207 Description 加里敦星球的人 ...