I want to inject a @SessionScoped
and a @RequestScoped
beans into my PhaseListener
implementation, but I got NullPointerException
.
I use tomcat with weld implementation to achieve CDI. I started to migrate JSF 2.2 to 2.3, so I changed to CDI from FacesContext
.
Well I replaced @ManagedBean
to @Named
and any other things to must do during migration like:
- add beans XML to every modules
- add BeanManager to context XML
- delete bean declarations from faces-config.xml
- add SPI BeanManager
as resource-env-ref to web.xml
How can I inject any bean to PhaseListener
implementations?
@Named
@SessionScoped
public class MyHandler implements Serializable {
..}
@Named
@RequestScoped
public class MyController extends MyParentController<Example> {
..}
public class MyPhaseListener implements PhaseListener {
private MyHandler myHandler;
private MyController myController;
@Inject
public void setMyHandler(MyHandler myHandler) {
this.myHandler= myHandler;
}
@Inject
public void setMyController (MyController myController) {
this.myController= myController;
}
...
public void afterPhase(PhaseEvent event) {
myHandler.method()
}
myHandler
injected bean is null in afterPhase
method.
Fortunately I solved the problem... A few things I had to do: