當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring构造方法注入类型歧义
生活随笔
收集整理的這篇文章主要介紹了
Spring构造方法注入类型歧义
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在Spring框架中,當(dāng)一個類包含多個構(gòu)造函數(shù)帶的參數(shù)相同,它總是會造成構(gòu)造函數(shù)注入?yún)?shù)類型歧義的問題。
問題
讓我們來看看這個客戶 bean 實例。它包含兩個構(gòu)造方法,均接受3個不同的數(shù)據(jù)類型參數(shù)。 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 methodspublic 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> 運行它,你期望的結(jié)果是什么? 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);} }輸出結(jié)果
name : yiibaiaddress : 28age : 188 其結(jié)果不是我們所期望的,第一個構(gòu)造器不執(zhí)行,而是第二構(gòu)造函數(shù)運行。在Spring參數(shù)類型'188'?能夠轉(zhuǎn)換成int,所以Spring只是轉(zhuǎn)換它,并采用第二個構(gòu)造來執(zhí)行,即使你認(rèn)為它應(yīng)該是一個字符串。 另外,如果Spring不能解決使用哪個構(gòu)造函數(shù),它會提示以下錯誤信息 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)解決
為了解決這個問題,應(yīng)該為構(gòu)造函數(shù)指定的確切數(shù)據(jù)類型,通過像這樣類型的屬性: <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> 再次運行它,現(xiàn)在得到你所期望的。輸出結(jié)果
name : yiibaiaddress : 188age : 28 注這是一個很好的做法,顯式聲明每個構(gòu)造函數(shù)參數(shù)的數(shù)據(jù)類型,以避免上述構(gòu)造注入型歧義的問題。 http://www.yiibai.com/spring/constructor-injection-type-ambiguities-in-spring.html總結(jié)
以上是生活随笔為你收集整理的Spring构造方法注入类型歧义的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无工作无流水怎么贷款 可以选择这2种方
- 下一篇: gradle idea java ssm