让我清楚一点,我是Spring framework
的完全初学者。
我有三个类文件,现在我在beans.xml中收到错误。您可以查看我的代码。
以下是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;
}
}
这是我的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());
}
}
这是我的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();
}
}
最后这是我的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>
我在beans.xml
文件中收到No constructor with 3 arguments defined in class
请帮忙,这是什么意思?
当然,帮助将不胜感激!!
答案 0 :(得分:4)
这个
public void Address(String city, String state, String address)
应该是
public MyAddress(String city, String state, String address)
您的构造函数中的类名错误,此外,构造函数没有返回类型。
Employee
有类似的错误:
public void Employee(int id, String name, MyAddress address)
应该是
public Employee(int id, String name, MyAddress address)
答案 1 :(得分:0)
地址类有一个默认构造函数。从方法中省略void
关键字。
答案 2 :(得分:0)
根据定义here, &#34;一个类包含被调用以从类蓝图创建对象的构造函数。构造函数声明看起来像方法声明 - 除了它们使用类的名称并且没有返回类型&#34;
答案 3 :(得分:0)
在MyAddress
类而不是创建构造函数中,您创建了Address
方法,
将public void Address(...)
更改为public MyAddress(...)
会使其正常工作