【发布时间】:2014-05-15 16:34:43
【问题描述】:
我无法摆脱错误:
HTTP Status 500 - Request processing failed; nested exception is
org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [[Lmain.java.com.springapp.mvc.model.DSLR;]:
No default constructor found;
nested exception is java.lang.NoSuchMethodException: [Lmain.java.com.springapp.mvc.model.DSLR;.<init>()
我实际上在 DSLR 类中有构造函数。我的代码有什么问题?
代码在这里> Spring MVC WebApp:http://goo.gl/ddhLg5
可能导致错误的 DSLR 类:
package main.java.com.springapp.mvc.model;
import org.springframework.beans.factory.annotation.Autowired;
public class DSLR {
public DSLR() {
}
private int dslrId;
private String model;
private int price;
private String description;
public int getDslrId() {
return dslrId;
}
public void setDslrId(int dslrId) {
this.dslrId = dslrId;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "DSLR [dslr=" + dslrId + ", model=" + model
+ ", price=" + price+ ", description=" + description+"]";
}
}
在实例化 DSLR 的 DSLRServletController 中,我进行了更改:
@ModelAttribute("dslrs") DSLR dslrs[]
改为:
@ModelAttribute("dslrs") 列出 dslrs
摆脱了先前的错误并给出了:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
已解决:Spring MVC web application: No default constructor found 如果有人可以在这里总结并写下答案,我很乐意接受!
【问题讨论】:
-
将
DSLR的包从main.java.com.springapp.mvc.model更改为com.springapp.mvc.model -
我为什么需要这样做?
-
因为
src/main/java是 maven 搜索要编译的类的位置,之后的任何内容都将是actual package -
如果我将
main.java.com.springapp.mvc.model更改为com.springapp.mvc.model,那么我将不得不将DSLR移动到其他目录,但我需要保持我的java 类井井有条。它与解决我的错误有什么关系? -
@SajanChandran 这根本不是问题......