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