尝试在构造函数中访问@Inject bean时出现NullPointerException

时间:2013-05-06 13:37:11

标签: jsf constructor nullpointerexception cdi managed-bean

我有一个会话范围的bean:

@Named
@SessionScoped
public class SessionBean implements Serializable {

    private String someProperty;

    public String getSomeProperty() {
        return someProperty;
    }

}

我想在请求范围的bean中注入它并用它初始化:

@Named
@RequestScoped
public class RequestBean {

    @Inject
    private SessionBean sessionBean;  

    public RequestBean() {
        System.out.println(sessionBean.getProperty());
    }

}

但是,它会抛出以下异常:

java.lang.NullPointerException
    at com.example.RequestBean.<init>(RequestBean.java:42)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.jboss.weld.introspector.jlr.WeldConstructorImpl.newInstance(WeldConstructorImpl.java:206)
    at org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:117)
    at org.jboss.weld.bean.ManagedBean.createInstance(ManagedBean.java:336)
    at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget.produce(ManagedBean.java:200)
    at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:292)
    ...

这是如何引起的?如何解决?

2 个答案:

答案 0 :(得分:22)

您希望在构造bean之前注入的依赖项可用。你期望它的工作原理如下:

RequestBean requestBean;
requestBean.sessionBean = sessionBean; // Injection.
requestBean = new RequestBean(); // Constructor invoked.

然而,这不是真的,技术上也是不可能的。 构造之后注入依赖项。

RequestBean requestBean;
requestBean = new RequestBean(); // Constructor invoked.
requestBean.sessionBean = sessionBean; // Injection.

如果您打算在构建bean之后直接根据注入的依赖项执行业务逻辑,那么您应该使用@PostConstruct方法。

删除构造函数并添加此方法:

@PostConstruct
public void init() {
    System.out.println(sessionBean.getSomeProperty());
}

答案 1 :(得分:1)

BalusC的回复是正确的,但确实反映了目前没有运行的对象创建的分配阶段。但无论如何CDI bean应该可以通过以下方式grep以编程方式访问:

javax.enterprise.inject.spi.CDI.current().select(SessionBean.class).get()