NullPointerException while trying to access @Inject bean in constructor

狂风中的少年 提交于 2019-11-26 09:43:57

问题


I\'ve a session scoped bean:

@Named
@SessionScoped
public class SessionBean implements Serializable {

    private String someProperty;

    public String getSomeProperty() {
        return someProperty;
    }

}

I\'d like to inject this in a request scoped bean and initialize with it:

@Named
@RequestScoped
public class RequestBean {

    @Inject
    private SessionBean sessionBean;  

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

}

However, it throws the following exception:

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)
    ...

How is this caused and how can I solve it?


回答1:


You're expecting that the injected dependency is available before the bean is constructed. You're expecting that it works like this:

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

This is however not true and technically impossible. The dependencies are injected after construction.

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

You should be using a @PostConstruct method instead if you intend to perform business logic based on injected dependencies directly after bean's construction.

Remove the constructor and add this method:

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



回答2:


BalusC's reply is correct, but is does reflect the assignment phase of a object creation, that did not run at this time. But anyway the CDI bean should be accessible if you grep it programatically via:

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


来源:https://stackoverflow.com/questions/16399974/nullpointerexception-while-trying-to-access-inject-bean-in-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!