(异常)Circular reference involving containing bean

Question

有次开发过程中,编译器内编译成功。Maven进行打包时报错Spring进行了@Autowired关键字进行了循环依赖。报错内容如下:

2017-09-27 14:35:31,021 [main] WARN  
[org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'systemConfig': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: cn.yanxml.test.service.SysDictionaryItemService cn.yanxml.test.config.SystemConfig.sysDictionaryItemService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysDictionaryItemService': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.yanxml.test.service.CmdbServiceService cn.yanxml.test.service.SysDictionaryItemService.cmdbServiceService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cmdbServiceService':
 Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.yanxml.test.service.UserService cn.yanxml.test.service.CmdbServiceService.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': 
 Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.yanxml.test.bean.Config cn.yanxml.test.service.UserService.config; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getCurrentSystemConfig' defined in class path resource [cn/yanxml/test/config/SystemConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [cn.yanxml.test.bean.Config]: Circular reference involving containing bean 'systemConfig' - consider declaring the factory method as static for independence from its containing instance. Factory method 'getCurrentSystemConfig' threw exception; nested exception is java.lang.NullPointerException
九月 27, 2017 2:35:31 下午 org.apache.catalina.core.StandardService stopInternal
信息: Stopping service Tomcat

Solution

消除循环依赖即可:

  • 去除循环依赖;
  • 如果为非复杂的逻辑关系,可以Service直接调用Dao层,而不要在Service层内进行循环调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值