在学习spring-boot2.3.0的过程中遇到了Spring-boot: required a bean named 'entityManagerFactory' that could not be found或A component required a bean named 'entityManagerFactory' that could not be found.
解决办法:
我收到错误消息,例如“找不到名为'entityManagerFactory'的bean”。经过大量的谷歌搜索解决了这个问题。我为JPA设置了手动配置。
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
return sessionFactory;
}
但是默认情况下,JPA通过名称'entityManagerFactory'搜索sessionFactory,因此将我的代码更改为:
@Bean(name="entityManagerFactory")
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
return sessionFactory;
}
可以手动设置 @Bean(name="entityManagerFactory") 也可以自动设置,就是方法名字一定要是entityManagerFactory
另一种解法
@Bean
public LocalSessionFactoryBean entityManagerFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
return sessionFactory;
}
网友评论