1
votes
        I have three files
        1) Test.java (in which i have created two parameterized constructor)
        2) spring.xml (xml file)
        3) Client.java(in which i have created main method)

        1)Test.java

        public class Test {
        private String name,email;
        private int age;

            public Test(String name,int age)
            {
                this.name=name;
                this.age=age;
            }
            public Test(String name)
            {
                this.name=name;
            }
            public void display()
            {
                System.out.println("First Constructor "+name+" "+age);
                System.out.println("Second Constructor "+name);

            }
        }



    2) spring.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

    <beans>
    <bean id="testObj" class="Test" >

    <constructor-arg value="name"  index="0"/>

    <constructor-arg value="123"  index="1"/>

    <constructor-arg value="name"/>

    </bean>
    </beans>

3) Client.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {

    public static void main(String[] args) {

        ApplicationContext context=new ClassPathXmlApplicationContext("resource/spring.xml");
        Test t1=(Test)context.getBean("testObj");
        t1.display();
    }

}

when i am calling only single constructor its working fine but in case of calling both of them its giving exception: Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testObj' defined in class path resource [resource/spring.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

2
I'm not sure what you are trying to do. Do you want to call two constructors at the same time? - Tunaki
yes. when i am calling only single constructor its working fine but in case of calling both of them its giving exception. - Däñish Shärmà
Well, what did you expect? You can only call one constructor to construct one object. - Tunaki

2 Answers

4
votes

The comments are right. The point of the the spring context file is to construct an instance of a class or a prototype that is constructed as needed to be injected as a dependency.

You only ever call one constructor to create an instance of an object (whether spring constructs your objects or you do it yourself in code).

You can always place multiple objects of the same class on the spring context, calling a different constructor in each case.

<bean id="testObj" class="Test" >
 <constructor-arg value="name" index="0"/>
 <constructor-arg value="123" index="1"/> 
 </bean>
<bean id="testObj1" class="Test" >
 <constructor-arg value="name"/> 
</bean>
0
votes

You can try parameterized JUnits instead. Where you will be able to provide as many test as you can with different test scenarios. You can check here