org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'mwsReceiveDetailService': Bean with name 'mwsReceiveDetailService' has been injected into other beans [commonController] 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.
时间: 2023-07-16 09:15:40 浏览: 362
这是Spring框架中的循环依赖问题。在你的代码中,`mwsReceiveDetailService`这个Bean被注入到了`commonController`中,并且`commonController`也被注入到了`mwsReceiveDetailService`中,形成了循环依赖。
解决这个问题的方法有两种:
1. 修改代码结构,避免循环依赖。可以把两个类中的依赖关系调整一下,比如提取一个接口,然后让`commonController`依赖这个接口而不是`mwsReceiveDetailService`本身。
2. 使用`@Lazy`注解进行懒加载。在注入`mwsReceiveDetailService`到`commonController`时,使用`@Lazy`注解告诉Spring容器在需要该Bean时再去实例化它,而不是在启动时就实例化它。这样可以避免循环依赖问题。示例代码如下:
```
@Service
public class CommonController {
private MwsReceiveDetailService mwsReceiveDetailService;
@Autowired
public void setMwsReceiveDetailService(@Lazy MwsReceiveDetailService mwsReceiveDetailService) {
this.mwsReceiveDetailService = mwsReceiveDetailService;
}
// ...
}
@Service
public class MwsReceiveDetailService {
private CommonController commonController;
@Autowired
public void setCommonController(CommonController commonController) {
this.commonController = commonController;
}
// ...
}
```
阅读全文