在根应用程序中使用自定义数据源和jpa EntityManager依赖时,抛出异常

时间:2018-03-02 09:13:08

标签: spring-boot

当我在我的@springbootapplication类中定义一个数据源并且在同一个类中干涉一个jpa entitymanager时,就像这样:

@Bean
@ConfigurationProperties("app.datasource")
public DataSource dataSource() {
          return DataSourceBuilder.create().build();
}

@Autowired
private EntityManager entityManager;

它将在启动过程中抛出异常:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:345)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367)
    ... 71 common frames omitted

当entityManager通过Autowired间接到根应用程序类时,也会出现这种情况 - >自动连接 - >自动装配Autowired 但是如果我将entityManager移动到另一个类(与Application类无关),例如:在注释基础bean的属性中,问题就会消失

通过堆栈跟踪,我认为这可能是一个bean依赖问题。 我不确定这是一个错误还是我的错误。 我使用的是springboot 2.0.0.RELEASE

的版本

1 个答案:

答案 0 :(得分:0)

response by him

这是你的错。您将实体管理器作为字段注入配置类。在配置类可以创建它定义的任何bean之前,必须注入它的所有字段。这会失败,因为使用实体管理器的配置类也提供了创建实体管理器所需的数据源。

将来,如果您认为自己犯了错误,请在Stack Overflow上提问。正如贡献指南中所述,我们更喜欢仅针对错误和增强功能使用GitHub问题。