记一次spring循环依赖

  1. 问题:spring循环依赖,

  2. 场景:A注入B,B注入A,
    按理来说spring是支持的处理,不会出现循环依赖的问题,但是,除了相互注入外,项目还是使用的AOP切面打印日志,使用了代理,问题就是出现在这里,

  3. 源码

	/**
	 * Whether to resort to injecting a raw bean instance in case of circular reference,
	 * even if the injected bean eventually got wrapped.
	 */
	private boolean allowRawInjectionDespiteWrapping = false;
  1. 解决方案:
@Component
public class MyWebApplicationContext implements BeanFactoryPostProcessor {


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        if(!(beanFactory instanceof DefaultListableBeanFactory))return;
        // todo:经过代理类后,循环依赖控制开关
        DefaultListableBeanFactory beanFactory1 = (DefaultListableBeanFactory)beanFactory;
        beanFactory1.setAllowRawInjectionDespiteWrapping(true);

    }
}
上一篇:Activiti6学习1——初识


下一篇:Spring5源码 - 05 invokeBeanFactoryPostProcessors 源码解读_2