admin管理员组

文章数量:1530059

今天写代码的时候遇到一个报错
 

nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'A': Bean with name 'A' has been injected into other beans [B] 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

经过科学上网排查,发现是spring 循环依赖了。如:

@Component
public class ServiceA {

    @Autowired
    private ServiceB serviceB;

    public ServiceA() {
        System.out.println("init serviceA");
    }
}

 



@Component
public class ServiceB {

    @Autowired
    ServiceA serviceA;

    public ServiceB() {
        System.out.println("innit ServiceB");
    }
}

互相依赖于对方,实例化时导致报错。

解决:我目前并没有解决循环依赖的问题,而是避免循环依赖。

如何解决sping的循环依赖,待定。

本文标签: wrappedeventuallyspingfinalbeans