jsfcdiweldjsf-2.3phaselistener

Is it possible to inject a bean to phaselistener when using JSF 2.3, tomcat with weld cdi implementation?


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.


Solution

  • Fortunately I solved the problem... A few things I had to do:

    1. I put beans.xml into META-INF/, but beans.xml has to be under WEB-INF.
    2. In beans.xml I had to change bean-discovery-mode from "annotated" to "all".
    3. I forgot to add a class with @FacesConfig(version = Version.JSF_2_3) annotation( to enable EL resolution for CDI beans).
    4. Also forgot to change faces-config.xml version to 2.3.