Reputation: 65
Let me clear it, I'm a completely beginner in Spring framework
.
I've three class files, Now i'm getting an error into beans.xml. You could take a look into my codes.
Here is MyAddress.java
:
package com.project;
public class MyAddress {
private String city;
private String state;
private String address;
public void Address(String city, String state, String address){
this.city=city;
this.state=state;
this.address=address;
}
public String toString(){
return city+" "+state+" "+address;
}
}
Here is my Employee.java
package com.project;
public class Employee {
private int id;
private String name;
private MyAddress address;
public Employee(){
System.out.print("Default constructor..");
}
public void Employee(int id, String name, MyAddress address){
this.id=id;
this.name=name;
this.address=address;
}
public void show(){
System.out.println(id+" "+name);
System.out.println(address.toString());
}
}
Here is my MainProgram.java
package com.project;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainProgram {
public static void main(String[] args){
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
Employee em=(Employee)ac.getBean("e");
em.show();
}
}
and finally here is my 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="e" class="com.project.MyAddress">
<constructor-arg value="USA" type="String"></constructor-arg>
<constructor-arg value="Delhi" type="String"></constructor-arg>
<constructor-arg value="Bangalore" type="String"></constructor-arg>
</bean>
<bean id="e2" class="com.project.Employee">
<constructor-arg value="123" type="int"></constructor-arg>
<constructor-arg value="raj"></constructor-arg>
<constructor-arg>
<ref bean="e"/>
</constructor-arg>
</bean>
</beans>
I'm getting an error in beans.xml
files as No constructor with 3 arguments defined in class
PLease help, what's that mean?
Surely, help would be appreciated!!
Upvotes: 1
Views: 1422
Reputation: 845
The address class has a default constructor. Omit void
keyword from method.
Upvotes: 0
Reputation: 341
In the MyAddress
class instead of creating a constructor you created a Address
method,
changing public void Address(...)
to public MyAddress(...)
will make it work
Upvotes: 0
Reputation: 99
As defined here, "A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type"
Upvotes: 0
Reputation: 394146
This
public void Address(String city, String state, String address)
should be
public MyAddress(String city, String state, String address)
You got the class name wrong in your constructor, and in addition, constructors don't have a return type.
You have a similar error for Employee
:
public void Employee(int id, String name, MyAddress address)
should be
public Employee(int id, String name, MyAddress address)
Upvotes: 4