Issue
I defined a data source as the following :
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:tcp://localhost/~/test");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource();
}
During spring's bootstrapping, console is throwing a massive:
Dez 07, 2016 5:00:53 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.h2.Driver
and after a while I'm getting
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class sample.config.AppConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource sample.config.AppConfig.dataSource()] threw exception; nested exception is java.lang.StackOverflowError
All the sample I've copied from some book, what can be wrong here?
Could the problem be, that I've put it into web application config class?
Solution
In your dataSource()
bean creation method change the return statement to:
return dataSource;
You are calling the method again, this is creating the exception.
Answered By - SachinSarawgi