简体   繁体   中英

@PostConstruct run when not all the properties of cascaded bean were initialized in Spring

For example I got class A, B, C

@Service
public class A {

    private B b;

    @PostConstruct
    public void initialize() {
        b.computeUsingC();
    }

    @Autowired
    public void setB(B b) {
        this.b = b;
    }
    ...
}

@Service
public class B {

    private C c;

    public computeUsingC() {
        c.compute()
    }

    @Autowired
    public void setC(C c) {
        this.c = c;
    }
    ...
}

@Service
public class C {
    private A a
    public void compute() {
        a.otherMethod();
    }
    ...
    public void setA(A a) {
        this.a = a;
    }
}

I got a NullpointException at line with: c.compute() when Spring starting up, because c is not get wired yet.
I think A should not run method with annotation @PostConstruct before B is get fully initialized. Although B get instantiated only for itself.

我发现我得到了A - > B - > C - > A的循环引用。所以Spring在使用它之前无法完全初始化B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM