【发布时间】:2014-06-13 22:31:18
【问题描述】:
谁能向我解释为什么第一种和第二种情况失败导致NullPointerException 因为b2 和/或b3 在Bean1 的构造函数中仍然为空,而第三种情况正常工作。
在所有情况下都有这个:
@Stateless
public class Bean2 {
@Inject
private Bean3 b3;
public Bean2(){
}
}
第一种情况:(失败)
@Singleton
@StartUp
public class Bean1 {
@Inject
private Bean2 b2;
public Bean1(){
b2.someMethod(); // b2 throws null pointer exception
}
}
第二种情况:(失败)
@Singleton
@StartUp
public class Bean1 {
private Bean2 b2;
public Bean1(){
b2 = new Bean2();
b2.someMethod(); // b3 throws null pointer exception
}
}
第三种情况:(成功)
@Singleton
@StartUp
public class Bean1 {
@Inject
private Bean2 b2;
public Bean1(){
}
@PostConstruct
public init(){
b2.someMethod();
}
}
【问题讨论】: