I am new to spring. I created one bean class and a configuration file as below :
Beans.xml
<?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">
<bean id="employee" class="com.asd.bean.Employee">
<constructor-arg index="0" type="java.lang.String" value="kuldeep" />
<constructor-arg index="1" type="java.lang.String" value="1234567" />
</bean>
</beans>
Employee.java
package com.asd.bean;
public class Employee {
private String name;
private String empId;
public Employee() {
System.out.println("Employee no-args constructor");
}
Employee(String name, String empId)
{
System.out.println("Employee 2-args constructor");
this.name=name;
this.empId=empId;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the empId
*/
public String getEmpId() {
return empId;
}
/**
* @param empId the empId to set
*/
public void setEmpId(String empId) {
this.empId = empId;
}
public String toString() {
return "Name : "+name+"\nEID : "+empId;
}
}
when I am trying to get bean using ApplicationContext it is giving following exception :
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employee' defined in class path resource [Problem.xml]: 2 constructor arguments specified but no matching constructor found in bean 'employee' (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Now if i removes the public from default constructor it works fine and even in case of making both constructors public also it is working. Please explain why it is showing this behaviour???
Thanks in Advance.
Constructoraccessible if it needs to.