Alan Hoo
Alan Hoo

Reputation: 455

Why ambiguity when I inject argument for multiple constructors?

Why does this happen when I define two constructors, one of which has referenced parameter, and another a primitive parameter?

Below are relevant snippets.

Constructor 1:

public TextEditor(SpellChecker sc) {...}

Constructor 2:

public TextEditor(int editorNum) {...}

bean definition xml file:

...
<constructor-arg ref="spellChecker"/> <!--spellChecker is defined as class elsewhere-->
<constructor-arg value="100"/>
...

Compiling error of ambiguities remains even when I add type/name/index to the tag.

post full stack trace below

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'textEditor' defined in class path resource [Beans.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:250)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.tutorialspoint.MainApp.main(MainApp.java:7)

Upvotes: 3

Views: 331

Answers (1)

Anish B.
Anish B.

Reputation: 16469

Proper constructor injection through applicationContext.xml will be like this :

For example : Suppose your TextEditor & SpellChecker classes are in the package com.test.

Now, the xml will look like :

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

    <!-- Create a SpellChecker bean -->
    <bean id = "spellChecker" class="com.test.SpellChecker"/>

    <!-- Create a TextEditor bean by calling the first constructor -->
    <bean id="textEditor" class="com.test.TextEditor" >
        <constructor-arg ref="spellChecker"/>
    </bean>

    <!-- Create a TextEditor bean by calling the second constructor -->
    <bean id="textEditor_1" class="com.test.TextEditor" >
        <constructor-arg value="100"/>
    </bean>

</beans>

Hope this helps you :)

Thanks.

Upvotes: 3

Related Questions