| 异常Circular reference involving containing bean

 | 异常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层内进行循环调用。

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部