未找到默认构造函数;嵌套异常是 java.lang.NoSuchMethodException bean 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37061773/
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-11-03 02:09:34  来源:igfitidea点击:

No default constructor found; nested exception is java.lang.NoSuchMethodException bean configuration

javaspringannotationsh2

提问by Charles Bryant

I am trying to run some sql on a H2 database, I believe my sql schema has run. Although I am not sure how to get a data source working. I have added the @Repository annotation and a @Autowired annotation for my data source setter, any help would be appreciated.

我正在尝试在 H2 数据库上运行一些 sql,我相信我的 sql 模式已经运行。虽然我不确定如何让数据源工作。我已经为我的数据源设置器添加了 @Repository 注释和 @Autowired 注释,任何帮助将不胜感激。

package ping.repositories;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import ping.cascade.integration.CascadeInterface;
import ping.cascade.integration.company.CascadeFactory;
import ping.domain.Cascade;
import ping.domain.PingRequestBody;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

@Repository
public class CascadeListRepository {

    private static final Logger log = LoggerFactory.getLogger(CascadeListRepository.class);

    private JdbcTemplate jdbcTemplate;
    private DataSource dataSource;

    private ArrayList<CascadeInterface> cascadeList;
    private PingRequestBody pingRequestBody;

    public CascadeListRepository(PingRequestBody pingRequestBody){
        this.cascadeList = new ArrayList<CascadeInterface>();
        this.pingRequestBody = pingRequestBody;
    }

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public ArrayList<CascadeInterface> getCascadeList(){

        CascadeFactory cascadeFactory = new CascadeFactory();



        jdbcTemplate.query(
                "SELECT cascade_id, cascade_name, cascade_object FROM cascade", 
                (rs, rowNum) -> new Cascade(rs.getInt("cascade_id"), rs.getString("cascade_name"), rs.getString("cascade_object"))
        ).forEach(cascade -> log.info(cascade.toString()));
        log.info("Finished sql");



        return cascadeList;
    }
}

And the exception is :

例外是:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cascadeListRepository' defined in URL [jar:file:/Users/charlesbryant/projects/java/spring/cb-ping-service/build/libs/cb-ping-service-0.1.0.jar!/ping/repositories/CascadeListRepository.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [ping.repositories.CascadeListRepository]: No default constructor found; nested exception is java.lang.NoSuchMethodException: ping.repositories.CascadeListRepository.<init>()
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar!/:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar!/:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar!/:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar!/:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar!/:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar!/:1.3.3.RELEASE]
    at ping.Application.main(Application.java:10) [cb-ping-service-0.1.0.jar!/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_31]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_31]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_31]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_31]
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:54) [cb-ping-service-0.1.0.jar!/:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_31]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [ping.repositories.CascadeListRepository]: No default constructor found; nested exception is java.lang.NoSuchMethodException: ping.repositories.CascadeListRepository.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    ... 23 common frames omitted
Caused by: java.lang.NoSuchMethodException: ping.repositories.CascadeListRepository.<init>()
    at java.lang.Class.getConstructor0(Class.java:3074) ~[na:1.8.0_31]
    at java.lang.Class.getDeclaredConstructor(Class.java:2170) ~[na:1.8.0_31]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80) ~[spring-beans-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]

回答by CConard96

Seems to make sense. You don't have a default constructor defined. IE:

似乎有道理。您没有定义默认构造函数。IE:

public CascadeListRepository() //No parameters makes this the default
{
}

回答by crabe

Not only do you not have a default contructor but the constructor you do have expects a PingRequestBody but you don't Autowire it so spring would never be able to automatically instantiate this class at the moment. Depending on your Spring Application Context you will either want to add a default constructor i.e. no arguments or add @Autowired to your existing constructor.

您不仅没有默认构造函数,而且您所拥有的构造函数都需要 PingRequestBody,但您没有自动装配它,因此目前 spring 永远无法自动实例化此类。根据您的 Spring 应用程序上下文,您可能想要添加一个默认构造函数,即没有参数,或者将 @Autowired 添加到您现有的构造函数中。

@Autowired
public CascadeListRepository(PingRequestBody pingRequestBody){
    this.cascadeList = new ArrayList<CascadeInterface>();
    this.pingRequestBody = pingRequestBody;
}

@Charles Bryant, In response to your comment below, why are you manually instantiating the CascadeListRepository in your controller? You have it annotated with @Repository so it should be a bean in your spring application context that you can simply @Autowire in your controller. If you manually instantiate it then the spring context isn't aware of it and has no control over it's lifecycle....

@Charles Bryant,为了回应您在下面的评论,您为什么要在控制器中手动实例化 CascadeListRepository?你已经用 @Repository 注释了它,所以它应该是你的 spring 应用程序上下文中的一个 bean,你可以在控制器中简单地 @Autowire 。如果您手动实例化它,那么 spring 上下文不知道它并且无法控制它的生命周期......