2017-05-07 93 views
0

我想從這個鏈接http://o7planning.org/en/10605/create-a-java-shopping-cart-web-application-using-spring-mvc-and-hibernate購物車的Spring MVC

,因爲它is.But我收到以下錯誤我已經使用了相同的代碼來實現這個購物車的例子。

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDBAuthenticationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.o7planning.springmvcshoppingcart.dao.AccountDAO org.o7planning.springmvcshoppingcart.authentication.MyDBAuthenticationService.accountDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory org.o7planning.springmvcshoppingcart.dao.impl.AccountDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'getSessionFactory' threw exception; nested exception is java.lang.NoSuchMethodError: org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.applyClassLoader(Ljava/lang/ClassLoader;)Lorg/hibernate/boot/registry/BootstrapServiceRegistryBuilder; May 07, 2017 12:31:59 PM org.springframework.web.context.ContextLoader initWebApplicationContext SEVERE: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDBAuthenticationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.o7planning.springmvcshoppingcart.dao.AccountDAO org.o7planning.springmvcshoppingcart.authentication.MyDBAuthenticationService.accountDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory org.o7planning.springmvcshoppingcart.dao.impl.AccountDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'getSessionFactory' threw exception; nested exception is java.lang.NoSuchMethodError: org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.applyClassLoader(Ljava/lang/ClassLoader;)Lorg/hibernate/boot/registry/BootstrapServiceRegistryBuilder; at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4851) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

任何人都可以幫助我解決這個問題嗎?

+0

在堆棧跟蹤,你會發現 「的NoSuchMethodError:org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.applyClassLoader(Ljava /朗/類加載器)Lorg /休眠/啓動/註冊表/ BootstrapServiceRegistryBuilder;」這對我來說似乎是一個類加載器問題 –

+0

我在示例中給出的pom.xml中使用了相同版本的hibernate jars,請你解釋如何解決類加載器問題? – user3744505

回答

0

更改休眠版,採用最新的休眠(pom.xml中):

<!-- Hibernate --> 
    <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-core --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>5.2.10.Final</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>5.2.10.Final</version> 
    </dependency> 


    <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-c3p0</artifactId> 
     <version>5.2.10.Final</version> 
    </dependency> 
+0

使用最新的休眠後得到同樣的錯誤,即使作者在該檢查中嚴格要求不使用休眠5 – user3744505

+0

我用它與最新的休眠5,它工作正常!請試試吧! – searching9x

0

的問題是ApplicationContextConfig.java class.That類中包含以下進口

import org.springframework.orm.hibernate5.HibernateTransactionManager; 
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; 

如果您想保留項目結構,因爲它將這些進口更改爲

import org.springframework.orm.hibernate4.HibernateTransactionManager; 
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; 

注意,我改變hibernate5到hibernate4以匹配依賴性版本在pom.xml

這應該是一個替代的解決方案。

如果有幫助,我希望聽到。

感謝認爲

+0

這是正確的答案,這工作,謝謝:) – user3744505