You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
AbstractBeanFactory类中: @OverRide
public Object getBean(String name) throws Exception {
BeanDefinition beanDefinition = beanDefinitionMap.get(name);
if (beanDefinition == null) {
throw new IllegalArgumentException("No bean named " + name + " is defined");
}
Object bean = beanDefinition.getBean();
if (bean == null) {
bean = doCreateBean(beanDefinition);
bean = initializeBean(bean, name); beanDefinition.setBean(bean);
}
return bean;
}
beanDefinition.setBean(bean);上面这行代码在原代码中没有这一行导致生成的对象非代理对象,aop不生效。但是加上之后循环引用有个问题。
A B两个类相互引用
假如AB 按顺序加载 那么调用的方法步骤为
A ->doCreateBean(beanDefinition);
B ->doCreateBean(beanDefinition);
B.setA(A) //此A为提前暴露在beanDefinitionMap中的A
B ->initializeBean(bean, name);
A.setB(B); //此时B为jdk生成的动态代理对象。
A ->initializeBean(bean, name);
AbstractBeanFactory类中:
@OverRide
public Object getBean(String name) throws Exception {
BeanDefinition beanDefinition = beanDefinitionMap.get(name);
if (beanDefinition == null) {
throw new IllegalArgumentException("No bean named " + name + " is defined");
}
Object bean = beanDefinition.getBean();
if (bean == null) {
bean = doCreateBean(beanDefinition);
bean = initializeBean(bean, name);
beanDefinition.setBean(bean);
}
return bean;
}
beanDefinition.setBean(bean);上面这行代码在原代码中没有这一行导致生成的对象非代理对象,aop不生效。但是加上之后循环引用有个问题。
A B两个类相互引用
假如AB 按顺序加载 那么调用的方法步骤为
A ->doCreateBean(beanDefinition);
B ->doCreateBean(beanDefinition);
B.setA(A) //此A为提前暴露在beanDefinitionMap中的A
B ->initializeBean(bean, name);
A.setB(B); //此时B为jdk生成的动态代理对象。
A ->initializeBean(bean, name);
以上步骤执行完成之后,A中的B是代理对象,但是B中的A还是提前暴露的对象,并没有更新成代理对象。这样的话引用就不一致了。不知道有什么办法可以解决这个问题吗。
The text was updated successfully, but these errors were encountered: