2016-02-05 71 views
0

我不知道AHY我得到這個錯誤錯誤名爲 '道' 創造豆:持久性的依賴注入失敗

主要

 
IBanqueMetier metier; 
     ClassPathXmlApplicationContext context 
     = new ClassPathXmlApplicationContext(
       new String[]{"applicationContext.xml"}); 
    metier = (IBanqueMetier) context.getBean("metier"); 
    metier.addClient(new Client("Client1","Adress1")); 

BanqueDaoImpl

 
public class BanqueDaoImpl implements IBanqueDao { 

    @PersistenceContext 
    private EntityManager em; 

    @Override 
    public Client addClient(Client c) { 
     // j'enregistre le client c et je le retourne 
     em.persist(c); 
     return c; 
    } 
.... 

和這第一個錯誤:

 
INFO : org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org[email protected]1be847c: startup date [Fri Feb 05 14:22:40 CET 2016]; root of context hierarchy 
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml] 
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.s[email protected]1846619: defining beans [dao,metier,dataSource,persistenceUnitManager,entityManagerFactory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy 
WARN : org.hibernate.ejb.HibernatePersistence - HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. 
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.s[email protected]1846619: defining beans [dao,metier,dataSource,persistenceUnitManager,entityManagerFactory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.annotations.common.reflection.java.JavaReflectionManager.injectClassLoaderDelegate(Lorg/hibernate/annotations/common/reflection/ClassLoaderDelegate;)V 
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:342) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93) 
    at toto.tata.titi.test.test.main(test.java:14) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.annotations.common.reflection.java.JavaReflectionManager.injectClassLoaderDelegate(Lorg/hibernate/annotations/common/reflection/ClassLoaderDelegate;)V 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:530) 
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:496) 
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:657) 
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:630) 
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:339) 
    ... 13 more 
Caused by: java.lang.NoSuchMethodError: org.hibernate.annotations.common.reflection.java.JavaReflectionManager.injectClassLoaderDelegate(Lorg/hibernate/annotations/common/reflection/ClassLoaderDelegate;)V 
    at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.generateDefaultReflectionManager(MetadataBuilderImpl.java:737) 
    at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.(MetadataBuilderImpl.java:709) 
    at org.hibernate.boot.internal.MetadataBuilderImpl.(MetadataBuilderImpl.java:127) 
    at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:135) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(EntityManagerFactoryBuilderImpl.java:185) 
    at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:34) 
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:165) 
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:160) 
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:135) 
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:50) 
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:268) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) 
    ... 26 more 

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
 
\t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
\t xmlns:tx="http://www.springframework.org/schema/tx" 
 
\t xmlns:context="http://www.springframework.org/schema/context" 
 
\t xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
 
\t \t \t \t \t \t http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
 
\t \t \t \t \t \t http://www.springframework.org/schema/context 
 
     \t \t \t \t \t \t http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
 

 
\t 
 
\t <bean id="dao" class="toto.tata.titi.dao.BanqueDaoImpl" /> 
 
\t <bean id="metier" class="toto.tata.titi.metier.BanqueMetierImpl"> 
 
\t \t <!-- injection de dependance --> 
 
\t \t <!-- on associe objet dao a objet metier --> 
 
\t \t <property name="dao" ref="dao"></property> 
 
\t </bean> 
 

 
\t 
 
\t <bean id="dataSource" 
 
\t \t class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
 
\t \t <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
 
\t \t <property name="url" value="jdbc:mysql://localhost:3306/banque5" /> 
 
\t \t <property name="username" value="root" /> 
 
\t \t <property name="password" value="" /> 
 
\t </bean> 
 

 
\t <bean id="persistenceUnitManager" 
 
\t \t class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"> 
 
\t \t <property name="persistenceXmlLocations"> 
 
\t \t \t <list> 
 
\t \t \t \t <value>persistence.xml</value> 
 
\t \t \t </list> 
 
\t \t </property> 
 
\t \t <property name="defaultDataSource" ref="dataSource"></property> 
 
\t </bean> 
 

 
\t <bean id="entityManagerFactory" 
 
\t \t class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
 
\t \t <property name="persistenceUnitManager" ref="persistenceUnitManager"></property> 
 
\t \t <property name="persistenceUnitName" value="MY_P_U"></property> 
 
\t </bean> 
 

 

 
\t <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
 
\t  <property name="entityManagerFactory" ref="entityManagerFactory"></property> 
 
\t </bean> 
 
\t 
 
\t <tx:annotation-driven transaction-manager="transactionManager"/> 
 
\t <context:annotation-config></context:annotation-config> 
 
</beans>

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?> 
 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
 
\t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
\t xsi:schemaLocation="http://java.sun.com/xml/ 
 
ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> 
 
\t <persistence-unit name="MY_P_U" transaction-type="RESOURCE_LOCAL"> 
 
     
 
\t \t <provider>org.hibernate.ejb.HibernatePersistence</provider> 
 
\t \t <properties> 
 
      
 
\t \t \t <property name="hibernate.show_sql" value="true" /> 
 
\t \t \t <property name="hibernate.hbm2ddl.auto" value="update" /> 
 
\t \t </properties> 
 
\t </persistence-unit> 
 
</persistence>

+0

你有一個 '的NoSuchMethodError':你很可能已經部署了不相容庫 –

+0

嘗試添加'@ Repository'above BanqueDaoImpl類聲明 – yugo

+0

您能列出您在此項目中使用的所有hibernate庫/版本嗎? (如果你有一個pom.xml請顯示)。 –

回答

-2

請,乾淨糾正所有錯誤和嵌套異常。我有很多類似的錯誤。我在我的應用程序上下文中註釋了這些bean,看起來它現在運行良好。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'licenseBean': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [] for bean with name '' defined in ServletContext resource [/WEB-INF/applicationContext-tree.xml]; nested exception is java.lang.ClassNotFoundException: 
0
在你的pom.xml

改變的Hibernate JPA 2.0版本到2.1

<dependency> 
     <groupId>org.hibernate.javax.persistence</groupId> 
     <artifactId>hibernate-jpa-2.1-api</artifactId> 
     <version>1.0.0.Final-redhat-1</version> 
    </dependency> 
相關問題