Spring Boot 2.0.2中没有类型为'java.util.Set <javax.persistence.entitymanager>'的合格bean

时间:2018-07-11 16:48:04

标签: java spring spring-boot

我有一个使用hibernate的Spring Boot Web应用程序,它在1.5.7.RELEASE上运行得很好。我决定升级到2.0.2。

我要做的就是更改SpringBootServletInitializer的位置。但是,当我部署到tomcat时,出现此错误:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jpaContext': Unsatisfied dependency expressed through constructor parameter 0; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Set<javax.persistence.EntityManager>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

我的属性文件或其他代码没有区别。我只是在POM中更改了我的春季版本,然后得到了错误。这是我的连接配置:

@Configuration
public class ConnectionsConfiguration implements ApplicationListener<ApplicationReadyEvent> {

static final Logger logger = LoggerFactory.getLogger(ConnectionsConfiguration.class);

@Autowired
DataSource dataSource;
@Autowired
private EntityManagerFactory emFactory;

@Bean
@Scope("prototype")
public LocalSessionFactoryBean sessionFactory() throws ClassNotFoundException {
    LocalSessionFactoryBean fact = new LocalSessionFactoryBean();
    fact.setAnnotatedPackages("com.****.persistence.model");
    fact.setPackagesToScan("com.****.persistence.model");
    fact.setDataSource(dataSource);
    return fact;
}

}

造成此错误的1.5.7和2.0.2有什么区别,我该如何解决?

1 个答案:

答案 0 :(得分:0)

不确定幕后的区别是什么,但是EntityManagerFactory的自动装配仍然有效,但是从该工厂获取EntityManager不再是默认设置。添加bean定义可以解决以下问题:

@Bean
public EntityManager getEntityManager() {
    return emFactory.createEntityManager();
} 

相对简单。