I have following classes:
@Repository
class A {
public void method1() {
...
}
}
@Component
class B implements C {
@Autowired
@Lazy
private A a;
public void method2() {
a.method1();
}
}
@Component
class D {
@Autowired
private List<C> c;
@PostConstruct
public void method3() {
// iterate on list c and call method2()
}
}
Let's suppose Spring initializes the beans as following:
1. First bean B is created. When bean B is being created, field a
will not be initialized because of the @Lazy
annotation.
2. Next bean D is created. Then method3()
will get executed as it is marked with @PostConstruct
, but bean A is not yet touched by Spring. So when a.method1() will be called, then will Spring create bean A and inject it into the field a
or will it throw a NullPointerException
?