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)