未找到默认构造函数;嵌套异常是 java.lang.NoSuchMethodException bean 配置
Posted
技术标签:
【中文标题】未找到默认构造函数;嵌套异常是 java.lang.NoSuchMethodException bean 配置【英文标题】:No default constructor found; nested exception is java.lang.NoSuchMethodException bean configuration 【发布时间】:2016-05-05 23:14:29 【问题描述】:我正在尝试在 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;
例外是:
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$1.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]
【问题讨论】:
【参考方案1】:似乎有道理。您没有定义默认构造函数。 即:
public CascadeListRepository() //No parameters makes this the default
【讨论】:
【参考方案2】:您不仅没有默认构造函数,而且您拥有的构造函数需要一个 PingRequestBody 但您没有自动装配它,因此 Spring 目前永远无法自动实例化此类。根据您的 Spring 应用程序上下文,您可能想要添加一个默认构造函数,即没有参数,或者将 @Autowired 添加到您现有的构造函数中。
@Autowired
public CascadeListRepository(PingRequestBody pingRequestBody)
this.cascadeList = new ArrayList<CascadeInterface>();
this.pingRequestBody = pingRequestBody;
@Charles Bryant,针对您在下面的评论,您为什么要在控制器中手动实例化 CascadeListRepository?你已经用@Repository 注释了它,所以它应该是你的spring应用程序上下文中的一个bean,你可以在你的控制器中简单地@Autowire。如果你手动实例化它,那么 spring 上下文不会意识到它并且无法控制它的生命周期......
【讨论】:
我实际上是从我的控制器实例化这个类,所以我发现这不会真正起作用。我不得不用数据源自动连接我的控制器,并通过构造函数传递它。我已经读过你可以通过调用应用程序上下文来访问它,我只看到你有 beans.xml 设置的例子,我没有,我不确定正确的语法/模式是什么?以上是关于未找到默认构造函数;嵌套异常是 java.lang.NoSuchMethodException bean 配置的主要内容,如果未能解决你的问题,请参考以下文章
构造函数抛出异常嵌套异常是 java.lang.nullpointerexception
构造函数抛出异常;嵌套异常是 java.lang.NoSuchMethodError: org.springframework.util.AntPathMatcher.setCaseSensitive
Spring-boot,无法自动装配类。未找到默认构造函数引发异常
java.rmi.UnmarshalException:解组参数错误;嵌套异常是:java.lang.ClassNotFoundException: ServicesTableau
spring boot oauth ExceptionHandlerExceptionResolver : NestedServletException: 嵌套异常是 java.lang.***Err