Spring中后置处理器与循环依赖的问题
在Spring的bean生命周期中, 我们可以通过实现BeanPostProcessor
来对bean初始化前后做操作, 在网上的许多帖子博客中, 不乏可以看见说Spring的AOP是在后置处理器中实现的, 这个理解显然有失偏颇, 很容易误导一般人在后置处理器中对原先的bean进行代理增强再返回替换掉原来的bean引用, 但这极容易引起一个如下error:
二月 13, 2023 9:14:43 下午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'bean1': Bean with name 'bean1' has been injected into other beans [bean2] 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 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.
Exception in thread "main" org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'bean1': Bean with name 'bean1' has been injected into other beans [bean2] 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 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:643)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:509)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:901)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:907)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:561)
at com.yadong.demo01.AnnotationConfigApplicationContextTest.main(AnnotationConfigApplicationContextTest.java:20)
这个错误只有在循环依赖产生时, 并且在后置处理替换掉了原来的bean并重新返回一个新的bean时会产生。
以下是demo示例:
目录结构:
Bean1.java
package com.yadong.demo01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Bean1 {
private String name = "bean1";
@Autowired
Bean2 bean2;
public Bean1(){
}
public Bean1(String name){
this.name = name;
}
public Bean2 getBean2() {
return bean2;
}
public void setBean2(Bean2 bean2) {
this.bean2 = bean2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Bean1{" +
"name='" + name + '\'' +
", bean2.name=" + bean2.getName() +
'}';
}
}
Bean2.java
package com.yadong.demo01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Component
public class Bean2 {
private String name = "bean2";
@Autowired
Bean1 bean1;
public Bean2() {
}
public Bean2(String name) {
this.name = name;
}
public Bean1 getBean1() {
return bean1;
}
public void setBean1(Bean1 bean1) {
this.bean1 = bean1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Bean2{" +
"name='" + name + '\'' +
", bean1.name=" + bean1.getName() +
'}';
}
}
BeanPostProcessor1.java
package com.yadong.demo01;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class BeanPostProcessor1 implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("[" + beanName + "]" + "初始化 前处理方法");
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("[" + beanName + "]" + "初始化 后处理方法");
return doInsteadBean(bean, beanName);
//return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
private Object doInsteadBean(Object bean, String beanName){
if(beanName.equals("bean1")){
Bean1 bean1 = new Bean1("替换后的对象bean1");
Bean2 bean2 = new Bean2("替换后的对象bean1.bean2");
bean1.setBean2(bean2);
return bean1;
}else{
return bean;
}
}
}
AnnotationConfigApplicationContextTest.java
package com.yadong.demo01;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author YadongTan
* @Description 循环依赖与bean生命周期测试
*/
public class AnnotationConfigApplicationContextTest {
public static void main(String[] args) {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(factory);
context.register(Config.class);
context.refresh();
Bean1 bean1 = context.getBean(Bean1.class);
System.out.println("bean1 = " + bean1);
Bean2 bean2 = context.getBean(Bean2.class);
System.out.println("bean2 = " + bean2);
}
@Configuration
@ComponentScan("com.yadong.demo01")
protected static class Config{
}
}
因为1ean1与bean2有循环依赖, 因此创建bean1过程中, 逻辑是:
(bean1)getBean()
-> doGetBean()
-> createBean()
-> doCreateBean()
-> getSingleton()
-> populateBean()
->… -> (bean2) getBean()
-> doGetBean()
-> … -> populateBean()
->对Bean2中的bean1字段进行属性填充,。
此时bean1这个bean会被调用getEarlyBeanReference()以提前将对象暴露出来, 在这里方法中, 会调用到WrapIfNecessary(), Spring会对这个bean判断是否需要被AOP增强从而创建代理, 一旦bean1的引用被bean2所持有, 后续在BeanPostProcessor替换掉原先的bean就会导致bean2属性中持有的bean1是旧版本的bean, 也就是没有持有最终版本的bean1(因为你在后置处理中替换掉了原来的bean)。
对于存在循环依赖又想对Bean进行手动代理的, 可以选择实现SmartInstantiationAwareBeanPostProcessor
接口的getEarlyBeanReference()
方法, 在这里返回代理后的bean。
package com.yadong.demo01;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class BeanPostProcessor3 implements SmartInstantiationAwareBeanPostProcessor {
@Override
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
return doInsteadBean(bean, beanName);
}
private Object doInsteadBean(Object bean, String beanName){
if(beanName.equals("bean1")){
Bean1 bean1 = new Bean1("替换后的对象bean1");
Bean2 bean2 = new Bean2("替换后的对象bean1.bean2");
bean1.setBean2(bean2);
return bean1;
}else{
return bean;
}
}
}