Framework Error
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myService': Cannot create inner bean 'myDAO' of type [com.example.MyDAO] while setting bean property 'myDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDAO': Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' found in the application context
What This Error Means
This error occurs when the Spring Framework is unable to create a bean (an object managed by the Spring IoC container) due to a dependency issue. In this case, it's a problem with the bean's dependency injection, specifically with the EntityManagerFactory.
Why It Happens
This error typically happens when there's a mismatch between the expected bean definitions in the application context and the actual definitions. It can also occur when the required dependencies, in this case, the EntityManagerFactory, are not properly registered or configured in the application context.
How to Fix It
- 1To fix this error, you need to ensure that the EntityManagerFactory is correctly registered in the application context. This can be done by adding the necessary configuration, such as the @EnableJpaRepositories annotation, or by manually creating a bean for the EntityManagerFactory using the @Bean annotation.
Example Code Solution
@SpringBootApplication
deep public class MyApplication {
private MyDAO myDAO;
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@Repository
public class MyDAO {
// ...
}@SpringBootApplication
deep public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@Configuration
public class DataConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setDataSource(dataSource());
bean.setPackagesToScan("com.example.entity");
return bean;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
return transactionManager;
}
}
@EnableJpaRepositories
public class RepositoryConfig {
// ...
}Fix for org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myService': Cannot create inner bean 'myDAO' of type [com.example.MyDAO] while setting bean property 'myDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDAO': Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' found in the application context
Browse Related Clusters
Related JAVA Errors
ORA-12545: TNS:Cannot register with TSLS (TNS Listener Service)
Error executing SQL query: Cannot insert explicit value for identity c
java.lang.StackOverflowError at com.example.Main.main(Main.java:15) -
Could not initialize Bean Validation provider for the constraint annot
Related JAVA Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error