简体   繁体   中英

Integration of Spring MVC with Hibernate using Maven

I have spent a lot of time trying to integrate Hibernate and Spring MVC. But again and again I got a problems, the last one is the most hard for me.

I created a simple Maven project and added to @Configuration-class new 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();
    }

After building of the project I sow strange output in console:

    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>)
...

All attempting to use links which involves controller lead to this:

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
    ...

Please, give me advice what to do to avoid this errors, because I have no ideas. Thanks

You have infinite recursion:

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

Change last line to:

return dataSource;

You can actually tell that by looking at the error log:

Factory method [public javax.sql.DataSource com.mvc.app.init.BaseConfig. dataSource() ] threw exception; nested exception is java.lang. StackOverflowError

Note that DriverManagerDataSource is not suitable for production use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM