Java 无法实例化 bean 类:BeanInstantiationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23684004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:40:49  来源:igfitidea点击:

Could not instantiate bean class: BeanInstantiationException

javaspring

提问by J.Olufsen

I cannot get rid of error:

我无法摆脱错误:

  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>()

I actually have constructor in DSLR class. What's wrong with my code?

我实际上在 DSLR 类中有构造函数。我的代码有什么问题?



Code here > Spring MVC WebApp: http://goo.gl/ddhLg5

代码在这里> Spring MVC WebApp:http: //goo.gl/ddhLg5



DSLR class which is supposedly causing error:

据称会导致错误的 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+"]";
    }
}


In DSLRServletController which instantiates DSLR I made changes:

在实例化 DSLR 的 DSLRServletController 中,我进行了更改:

@ModelAttribute("dslrs") DSLR dslrs[]

@ModelAttribute("dslrs") DSLR dslrs[]

changed to:

变成:

@ModelAttribute("dslrs") List dslrs

@ModelAttribute("dslrs") 列出 dslrs

which got rid of previous error and gave:

它摆脱了以前的错误并给出了:

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface



SOLVEDhere: Spring MVC web application: No default constructor foundIf anyone can summarize and write the answer here I'd be happy to accept!

在这里解决Spring MVC Web 应用程序:找不到默认构造函数如果有人可以在这里总结并写下答案,我很乐意接受!

采纳答案by Qix - MONICA WAS MISTREATED

In order for certain frameworks to initialize objects in such a way, you have to provide a default constructor(a constructor that takes no arguments), even if it doesn't do anything.

为了让某些框架以这种方式初始化对象,您必须提供一个默认构造函数(一个不带参数的构造函数),即使它什么都不做

This is due to the fact you probably have another constructor in there that takes at least one argument. Logically, the library doesn't know what arguments to pass to every arbitrary class you pass it.

这是因为您可能有另一个构造函数,它至少接受一个参数。从逻辑上讲,库不知道将哪些参数传递给您传递给它的每个任意类。

This is indicated in the error where it says java.lang.NoSuchMethodException: [Lmain.java.com.springapp.mvc.model.DSLR;.<init>():

这在错误中指出java.lang.NoSuchMethodException: [Lmain.java.com.springapp.mvc.model.DSLR;.<init>()

  • NoSuchMethodExceptionmeans it can't find a method it expects at runtime (through reflection)
  • .<init>()refers to the constructor (constructors don't technicallyhave names, since they're always just the name of the class itself; therefore, the JVM refers to them as <init>()).
  • NoSuchMethodException意味着它在运行时找不到它期望的方法(通过反射)
  • .<init>()指的是构造函数(构造函数在技术上没有名称,因为它们始终只是类本身的名称;因此,JVM 将它们称为<init>())。

回答by Karibasappa G C

As the error is impossible with the DSLRclass you shared with us, my guess is that your server might contain an old class.

由于DSLR您与我们共享的类不可能出现错误,我猜测您的服务器可能包含一个旧类。

I suggest cleaning the project as well as the Tomcat installation from within your IDE.

我建议从您的 IDE 中清理项目以及 Tomcat 安装。

  • Eclipse: Project -> Clean ...
  • Tomcat: Right click on the server in Servers view -> Clean ...
  • Eclipse:项目-> 清理...
  • Tomcat:在服务器视图中右键单击服务器 -> 清理 ...