在Hibernate 5中如何自动连接SessionFactory?

我尝试autowire SessionFactory,但得到这个错误:

APPLICATION FAILED TO START *************************** Description: Field bookRepository in com.test.app.BookService required a bean named 'entityManagerFactory' that could not be found. 

在这里我自动装配SessionFactory:

 @Service class TestClass{ @Autowired lateinit var sessionFactory: SessionFactory ...... } 

这是我的配置类:

 @Configuration class SpringConfig { @Bean fun sessionFactory(emf: EntityManagerFactory): HibernateJpaSessionFactoryBean { val fact = HibernateJpaSessionFactoryBean() fact.entityManagerFactory = emf return fact } } 

我尝试这种方式来:

 @Configuration class SpringConfig { @Bean fun sessionFactory(): HibernateJpaSessionFactoryBean { return HibernateJpaSessionFactoryBean() } } 

我的application.yml:

 spring: datasource: driver-class-name: org.postgresql.Driver url: datasource_url password: password username: username hikari: maximum-pool-size: 5 jpa: properties: hibernate: jdbc: batch_size: 20 current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext 

休眠版本:5.4.1

Spring启动版本:2.0.0.M3