spring-boot - Spring Boot 2.0.x - @Configuration 类 @Autowired entityManagerFactory NullPointerException
问题描述
我使用 Hibernate 会话从 MySQL 查询数据。我使用 JPA EntityManagerFactory 来获取 Hibernate SessionFactory:
@Configuration
public class DatabaseConfig {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public SessionFactory getSessionFactory() {
if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("Factory is not a hibernate factory");
}
return entityManagerFactory.unwrap(SessionFactory.class);
}
}
这适用于 Spring Boot 1.5.x。但是当我将 Spring Boot 更新到 2.0.1 时:
创建名称为“databaseConfig”的 bean 时出错:通过字段“entityManagerFactory”表示的依赖关系不满足;
我应该怎么做才能在 Spring Boot 2.0.x 中解决这个问题?
解决方案
推荐阅读
- static - 具有自定义源和图层样式的 MapBox 静态地图
- apache-kafka-streams - Kafka Streams 到多个输出主题:对交付保证的影响
- python - 终端错误:使用序列设置数组元素
- html - 带有媒体查询的居中栏
- javascript - 获取上次更新 github 存储库的时间
- python - 使用python选择json中的特定键
- google-compute-engine - 如何使用 Google Compute Engine 实例创建传出连接负载平衡器
- android - 使用 json 获取 Spinner android 中显示的值的唯一 ID
- jboss - javax.naming.NameNotFoundException: EJB 未绑定 - jboss 6.1.0 | ejb2.1
- javascript - 在 pxp-ui 中,如何在表格中显示描述字段而不是 Id