我试图在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]
最佳答案
似乎很有意义。您没有定义默认的构造函数。
IE浏览器:
public CascadeListRepository() //No parameters makes this the default
{
}
关于java - 找不到默认的构造函数;嵌套异常是java.lang.NoSuchMethodException bean配置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37061773/