Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to inject EJB into JSF phase listener

I'm working with a Wildfly 10 server and I'm having issues with a JSF phase listener not getting an EJB injected to it. Here is my test code that I can't seem to get to work right.

public class TestListener implements PhaseListener {
@EJB
BasicEJB bjb;

private final static Logger LOGGER = Logger.getLogger(TestListener.class.getName());

@Override
public void afterPhase(PhaseEvent arg0) {
    LOGGER.log(Level.INFO, "After Restore View event hook called.");

}

@Override
public void beforePhase(PhaseEvent arg0) {
    LOGGER.log(Level.INFO, "Before Restore View event hook called.");
    bjb.callMe(); // crash happens right here.
}

@Override
public PhaseId getPhaseId() {
    return PhaseId.RESTORE_VIEW;
}
}

If I comment out the bjb.callMe(); line, the program works like normal. With it, I get a NullPointerException. I am able to inject this EJB and use it in a @RequestScoped backing bean.

I've learned that before JSF 2.2, you couldn't inject into a Phase Listener, however, I'm on JSF 2.2.12 with this server. Whats more, I can deploy this exact code on a Glassfish4 server and it works. What am I doing wrong with Wildfly?

like image 554
Psirax Avatar asked Dec 05 '25 07:12

Psirax


1 Answers

Injecting an @EJB in a JSF PhaseListener on Wildfly is apparently bugged at the moment. See https://developer.jboss.org/thread/269770 for reference.

However, if I change the annotation to @Inject instead of @EJB, it works just fine. (Thanks to BalusC and the JBoss forum contributors for that suggestion)

like image 53
Psirax Avatar answered Dec 08 '25 00:12

Psirax