当我启动spring应用程序时,我收到了BeanCurrentlyInCreationException。
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.datayes.bdb.rrp.business.service.impl.StockModelBaseService com.datayes.bdb.rrp.business.service.impl.StockModelV3ServiceImpl.stockModelBaseService; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'stockModelBaseService': Bean with name 'stockModelBaseService' has been injected into other beans [researchFrameworkCommonServiceImpl] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 71 more
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'stockModelBaseService': Bean with name 'stockModelBaseService' has been injected into other beans [researchFrameworkCommonServiceImpl] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:568)
以下是我发现导致此问题的代码段:
@Component public class KafkaConsumerServer implements MessageListener<String, String> {
@Autowired StockModelV3Service stockModelV3Service;
@Autowired FinanceIndicatorService financeIndicatorService;
......
}
@Service public class FinanceIndicatorServiceImpl implements FinanceIndicatorService {
@Autowired StockModelV3Service stockModelV3Service;
@Autowired IndustryResearchFrameworkService industryResearchFrameworkService;
......
}
@Service public class IndustryResearchFrameworkServiceImpl implements IndustryResearchFrameworkService {
@Autowired ResearchFrameworkCommonService commonService;
......
}
@Service public class ResearchFrameworkCommonServiceImpl implements ResearchFrameworkCommonService {
@Autowired StockModelBaseService stockModelBaseService;
......
}
答案 0 :(得分:1)
我在下面的文章中发现了一些有趣的东西可能已经解释了原因。 https://blog.imaginea.com/spring-bean-creation-is-not-thread-safe/
由于我的StockModelV3Service和FinanceIndicatorService都依赖于stockModelBaseService(FinanceIndicatorService - &gt; industryResearchFrameworkService - &gt; researchFrameworkCommonService - &gt; stockModelBaseService),因此在Spring bean创建过程中,它们相互竞争。这导致了BeanCurrentlyInCreationException。正如上面提到的那样,spring bean创建不是线程安全的。
解决这个问题似乎很容易。我改变了自动装配顺序如下:
public class KafkaConsumerServer implements MessageListener<String, String> {
@Autowired FinanceIndicatorService financeIndicatorService;
@Autowired StockModelV3Service stockModelV3Service;
由于FinanceIndicatorService也依赖于StockModelV3Service,所以当加载financeIndicatorService时,它会等到stockModelV3Service被加载然后加载自己。因此,避免使用BeanCurrentlyInCreationException。
毕竟,它与循环引用无关。由于StockModelBaseService不依赖于其他服务。