Java Spring DriverManager数据源-驱动程序加载期间堆栈溢出

Java Spring DriverManager数据源-驱动程序加载期间堆栈溢出,java,spring,spring-mvc,datasource,spring-bean,Java,Spring,Spring Mvc,Datasource,Spring Bean,我将数据源定义为以下内容: @Bean public DataSource dataSource(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUrl("jdbc:h2:tcp://localhost/~/test"); dataSource.se

我将数据源定义为以下内容:

@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();
}
在spring的引导过程中,console将抛出一个巨大的:

Dez 07, 2016 5:00:53 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.h2.Driver
过了一段时间,我开始

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
我从某本书中复制的所有样本,这里可能有什么问题

问题可能是,我已将其放入web应用程序配置类中?

dataSource()
bean创建方法中,将return语句更改为:

return dataSource;
您再次调用该方法,这将创建异常。

dataSource()
bean创建方法中,将return语句更改为:

return dataSource;

您再次调用该方法,这是在创建异常。

错误在于您正在递归调用自己的方法,最后它将通过StackOverflower错误在于您正在递归调用自己的方法,最后它将通过StackOverflower错误。

您的libs文件夹中是否有h2驱动程序jar文件?是,我添加了一个h2 maven依赖项,驱动程序在libs中。请将dataSource()编辑到dataSource。当然!多么愚蠢的错误!多谢各位!请关闭或删除问题-我无法回答(因为已经有答案)。代码中有一个基本的编程错误,在Spring中初始化的主题中没有添加任何内容。您的libs文件夹中是否有h2驱动程序jar文件?是的,我添加了h2 maven依赖项,并且驱动程序位于libs中。请将dataSource()编辑到dataSource。当然!多么愚蠢的错误!多谢各位!请关闭或删除问题-我无法回答(因为已经有答案)。代码中有一个基本的编程错误,在Spring中初始化的主题中没有添加任何内容。