roh*_*wal 2 java spring annotations lazy-evaluation postconstruct
我有以下课程:
@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()
}
}
Run Code Online (Sandbox Code Playgroud)
让我们假设Spring将bean初始化如下:
1.创建第一个bean B. 当创建bean B时,a由于@Lazy注释,将不会初始化字段.
2.创建下一个bean D. 然后method3()将被标记为执行@PostConstruct,但Spring尚未触及bean A. 因此,当调用a.method1()时,Spring会创建bean A并将其注入字段中a还是会抛出NullPointerException?
| 归档时间: |
|
| 查看次数: |
4148 次 |
| 最近记录: |