Reputation: 1015
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.
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$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]
Upvotes: 2
Views: 18221
Reputation: 332
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.
@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....
Upvotes: 3
Reputation: 914
Seems to make sense. You don't have a default constructor defined. IE:
public CascadeListRepository() //No parameters makes this the default
{
}
Upvotes: 4