Constructor injection type ambiguities in Spring

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.

<?xml version="1.0"?>
<!--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 :

<?xml version="1.0"?>
<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

NoteIt’s always a good practice to explicitly declared the data type for each constructor argument, to avoid constructor injection type ambiguities issue above.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末贞谓,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件喷众,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機滤蝠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來授嘀,“玉大人物咳,你說我怎么就攤上這事√阒澹” “怎么了览闰?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長巷折。 經常有香客問我压鉴,道長,這世上最難降的妖魔是什么锻拘? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任油吭,我火速辦了婚禮,結果婚禮上逊拍,老公的妹妹穿的比我還像新娘上鞠。我一直安慰自己,他們只是感情好芯丧,可當我...
    茶點故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布芍阎。 她就那樣靜靜地躺著,像睡著了一般缨恒。 火紅的嫁衣襯著肌膚如雪谴咸。 梳的紋絲不亂的頭發(fā)上轮听,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天,我揣著相機與錄音岭佳,去河邊找鬼血巍。 笑死,一個胖子當著我的面吹牛珊随,可吹牛的內容都是我干的述寡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼叶洞,長吁一口氣:“原來是場噩夢啊……” “哼鲫凶!你這毒婦竟也來了?” 一聲冷哼從身側響起衩辟,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤螟炫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后艺晴,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體昼钻,經...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年封寞,在試婚紗的時候發(fā)現(xiàn)自己被綠了然评。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡钥星,死狀恐怖沾瓦,靈堂內的尸體忽然破棺而出满着,到底是詐尸還是另有隱情谦炒,我是刑警寧澤,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布风喇,位于F島的核電站宁改,受9級特大地震影響,放射性物質發(fā)生泄漏魂莫。R本人自食惡果不足惜还蹲,卻給世界環(huán)境...
    茶點故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望耙考。 院中可真熱鬧谜喊,春花似錦、人聲如沸倦始。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鞋邑。三九已至诵次,卻和暖如春账蓉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背逾一。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工铸本, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人遵堵。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓箱玷,卻偏偏與公主長得像,于是被迫代替她去往敵國和親陌宿。 傳聞我的和親對象是個殘疾皇子汪茧,可洞房花燭夜當晚...
    茶點故事閱讀 44,619評論 2 354

推薦閱讀更多精彩內容