Spring data jpa- Nessun bean denominato ‘entityManagerFactory’ è definito; Iniezione delle dipendenze autowired fallita

Sto sviluppando un’applicazione utilizzando i dati di spring jpa, hibernate, mysql, tomcat7, maven e si tratta di errore di creazione. Sto cercando di capirlo, ma ho fallito.

errore non può risolvere il riferimento a bean ‘entityManagerFactory’ durante l’impostazione dell’argomento costruttore; Nessun bean denominato ‘entityManagerFactory’ è definito; Iniezione delle dipendenze autowired fallita

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'initDbService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.wahid.cse.repository.RoleRepository org.wahid.cse.service.InitDbService.roleRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': Cannot create inner bean '(inner bean)#c08f81' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#c08f81': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.wahid.cse.repository.RoleRepository org.wahid.cse.service.InitDbService.roleRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': Cannot create inner bean '(inner bean)#c08f81' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#c08f81': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) ... 22 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': Cannot create inner bean '(inner bean)#c08f81' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#c08f81': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:290) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:129) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) ... 24 more 

ecco il mio file ApplicationContext.xml

                 true create org.hibernate.dialect.MySQLDialect           

Ecco pom.xml

  4.0.0 org.ferdous.wahid java-blog-aggregator 0.0.1-SNAPSHOT war   4.0.5.RELEASE 3.0.4 3.1.0    org.apache.commons commons-dbcp2 2.0.1   mysql mysql-connector-java 5.1.31   org.springframework.data spring-data-jpa 1.6.1.RELEASE   org.hibernate hibernate-entitymanager 4.3.5.Final   javax.servlet javax.servlet-api ${servlet-api-version} provided  <!--  javax.servlet.jsp javax.servlet.jsp-api 2.2.1  -->  jstl jstl 1.2   org.apache.tiles tiles-core ${apache.tiles}   org.apache.tiles tiles-jsp ${apache.tiles}   org.slf4j slf4j-log4j12 1.7.7    org.springframework spring-core ${org.springframework.version}    org.springframework spring-expression ${org.springframework.version}    org.springframework spring-beans ${org.springframework.version}    org.springframework spring-aop ${org.springframework.version}    org.springframework spring-context ${org.springframework.version}    org.springframework spring-context-support ${org.springframework.version}    org.springframework spring-tx ${org.springframework.version}    org.springframework spring-jdbc ${org.springframework.version}    org.springframework spring-orm ${org.springframework.version}    org.springframework spring-oxm ${org.springframework.version}    org.springframework spring-web ${org.springframework.version}    org.springframework spring-webmvc ${org.springframework.version}    org.springframework spring-webmvc-portlet ${org.springframework.version}    org.springframework spring-test ${org.springframework.version} test      org.apache.tomcat.maven tomcat7-maven-plugin 2.2   org.apache.maven.plugins maven-compiler-plugin 3.1  1.7 1.7     

RoleRepository is

 import org.springframework.data.jpa.repository.JpaRepository; import org.wahid.cse.entity.Role; public interface RoleRepository extends JpaRepository{ } 

Spring Data JPA per impostazione predefinita cerca EntityManagerFactory denominato entityManagerFactory . EnableJpaRepositories un’occhiata a questa parte di Javadoc di EnableJpaRepositories o alla Table 2.1 della documentazione JPA di Spring Data.

Ciò significa che devi rinominare il tuo emf bean in entityManagerFactory o cambiare la tua configurazione di Spring in:

  

(se stai usando XML)

o

@EnableJpaRepositories(basePackages="your.package", entityManagerFactoryRef="emf")

(se stai usando Java Config)

Nel contesto dell’applicazione, cambia il bean con id da emf a entityManagerFactory :

      true create org.hibernate.dialect.MySQLDialect       

A

      true create org.hibernate.dialect.MySQLDialect       

Ho avuto questo problema dopo la migrazione da spring-boot-starter-data-jpa ver. Da 1.5.7 a 2.0.2 (dalla vecchia ibernazione alla ibernazione 5.2). Nella mia class @Configuration ho iniettato entityManagerFactory e transactionManager .

 //I've got my data source defined in application.yml config file, //so there is no need to configure it from java. @Autowired DataSource dataSource; @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { //JpaVendorAdapteradapter can be autowired as well if it's configured in application properties. HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(false); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); //Add package to scan for entities. factory.setPackagesToScan("com.company.domain"); factory.setDataSource(dataSource); return factory; } @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory); return txManager; } 

Ricordarsi inoltre di aggiungere la dipendenza da hibernate-entitymanager a pom.xml altrimenti non si troverà EntityManagerFactory su classpath:

  org.hibernate hibernate-entitymanager 5.0.12.Final  

Penso che questo sia correlato alla nuova versione di avvio primaverile più usando i dati primaverili JPA sostituisca appena l’annotazione @Bean sopra public LocalContainerEntityManagerFactoryBean entityManagerFactory () a @Bean (name = “entityManagerFactory”)

Determinare il nome del bean dovrebbe risolvere il problema