Tag Archives: Cannot resolve reference to bean ‘entityManagerFactory’ while setting constructor argument

Cannot resolve reference to bean ‘entityManagerFactory’ while setting constructor argument

spring 5.+jpa configuration class error.

December 20, 2018 5:53:01 PM org.springframework.web.servlet.DispatcherServlet initServletBean
Seriously : Context initialization failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘studentRepository’: Cannot create inner bean ‘(inner bean)#69ecc373’ 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)#69ecc373’: Cannot resolve reference to bean ‘entityManagerFactory’ while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘entityManagerFactory’ available

One of the reasons for this is the incorrect method name in the configuration class.

@Bean
	public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean()
	{
		//EntityManagerFactory
		LocalContainerEntityManagerFactoryBean bean=new LocalContainerEntityManagerFactoryBean();		
		bean.setDataSource(dataSource());
		bean.setPackagesToScan(new String[] {"com.jpa.demo"});
		bean.setJpaVendorAdapter(jpaVendorAdapter());
		return bean;
	}
@Bean
	public LocalContainerEntityManagerFactoryBean entityManagerFactory()
	{
		//EntityManagerFactory
		LocalContainerEntityManagerFactoryBean bean=new LocalContainerEntityManagerFactoryBean();		
		bean.setDataSource(dataSource());
		bean.setPackagesToScan(new String[] {"com.jpa.demo"});
		bean.setJpaVendorAdapter(jpaVendorAdapter());
		return bean;
	}