Error creating bean with name '#': Bean with name '#' has been injected into other beans [¥] 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.
出现这种错误就是有两个类循环依赖,如
public class GoodA{
@Autowired
private GoodB goodb;
}
public class GoodB{
@Autowired
private GoodA gooda;
}
这种情况就是循环依赖,解决办法有两种
1.在其中一个或者两个@Autowired下加上@Lazy
2.在其中一个或者两个Bean前加上@Lazy
//第一种方法
public class GoodA{
@Autowired
@Lazy //1.1
private GoodB goodb;
}
public class GoodB{
@Autowired
@Lazy //加在其中一个地方就可以解决循环依赖问题
private GoodA gooda;
}
//第二种方法
@Lazy
public class GoodA{
@Autowired
private GoodB goodb;
}
@Lazy//加在其中一个地方就可以解决循环依赖问题
public class GoodB{
@Autowired
private GoodA gooda;
}
有一种情况是加不成功的,那就是链式调用的时候,A调用B的时候必须,想要使其懒加载就必须在A的前面加上@Lazy,在B前加@Lazy是不会起作用的。