[英]Integration of Spring MVC with Hibernate using Maven
I have spent a lot of time trying to integrate Hibernate and Spring MVC. 我花了很多时间尝试集成Hibernate和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: 我创建了一个简单的Maven项目并添加到@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.
工厂方法[public javax.sql.DataSource com.mvc.app.init.BaseConfig。 dataSource() ] threw exception;
dataSource() ]抛出异常; nested exception is java.lang.
嵌套异常是java.lang。 StackOverflowError
的StackOverflowError
Note that DriverManagerDataSource
is not suitable for production use. 请注意,
DriverManagerDataSource
不适合生产使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.