1

我花了很多时间尝试集成 Hibernate 和 Spring MVC。但我一次又一次遇到问题,最后一个对我来说是最难的。

我创建了一个简单的Maven 项目并添加到 @Configuration-class 新 bean:

@Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
        dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
        dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
        dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));

        return dataSource();
    }

构建项目后,我在控制台中播下奇怪的输出:

    at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:280)
    at com.mvc.app.init.BaseConfig$$EnhancerByCGLIB$$14f5e201.dataSource(<generated>)
    at com.mvc.app.init.BaseConfig.dataSource(BaseConfig.java:43)
    at com.mvc.app.init.BaseConfig$$EnhancerByCGLIB$$14f5e201.CGLIB$dataSource$1(<generated>)
    at com.mvc.app.init.BaseConfig$$EnhancerByCGLIB$$14f5e201$$FastClassByCGLIB$$49506a23.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:280)
    at com.mvc.app.init.BaseConfig$$EnhancerByCGLIB$$14f5e201.dataSource(<generated>)
    at com.mvc.app.init.BaseConfig.dataSource(BaseConfig.java:43)
    at com.mvc.app.init.BaseConfig$$EnhancerByCGLIB$$14f5e201.CGLIB$dataSource$1(<generated>)
...

所有尝试使用涉及控制器的链接都会导致:

exception

javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)

root cause

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class com.mvc.app.init.BaseConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.mvc.app.init.BaseConfig.dataSource()] threw exception; nested exception is java.lang.StackOverflowError
    ...

请给我建议如何避免这个错误,因为我没有想法。谢谢

4

1 回答 1

4

你有无限递归:

public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    //...
    return dataSource();
}

将最后一行更改为:

return dataSource;

您实际上可以通过查看错误日志来判断:

工厂方法 [public javax.sql.DataSource com.mvc.app.init.BaseConfig. dataSource() ] 抛出异常;嵌套异常是 java.lang。堆栈溢出错误

注意DriverManagerDataSource不适合生产使用。

于 2012-12-02T21:36:15.320 回答