0

I am using jsf2.2 with wildfly 8.1 and javaee7.

My CDI bean injection in the phaselistener works as expected, but the @PostConstuct method is never invocked

I have tried to annotate the phaselistener with @javax.enterprise.context.ApplicationScope, SessionScope and Dependent to no avail.

Apart from naming, this is the exact thing i do in my post construct.

//@ApplicationScope
//@SessionScope
//@Dependent
public class MyPhaseListener implements PhaseListener {

   @Inject
   @Any
   private Instance<MyOrderedUrlHandler> myOrderedUrlhandlers;
   private Map<String, List<MyOrderedUrlHandler> orderedUrlHandlersMap;

   @PostConstruct
   void mapOrderedUrlHandlers() {
      LOG.info("Executing postconstruct");
      orderedUrlHandlersMap = Maps.newHashMap();

      for(final MyOrderedUrlHandler urlhandler : myOrderedUrlhandlers) {
         final String handles = urlhandler.url();
         final List<MyOrderedUrlHandler> registeredHandlers = orderedUrlHandlersMap.get(handles);

         if(registeredHandlers == null) {
            registeredHandlers = Lists.newArraList();
         }
         registeredHandlers.add(urlHandler);
         orderedUrlHandlersMap.put(handles, registeredHandlers);
      }
   }
}

Method level injection also works fine.

Is it the case that @PostConstruct callback is not part of jsf phaselistener specs?

maress
  • 3,533
  • 1
  • 19
  • 37
  • Show your code. You might be breaking the contract for `@PostConstruct` – kolossus Feb 03 '15 at 02:49
  • What if you and an explicit default constructor? – Kukeltje Feb 03 '15 at 09:20
  • @Kukeltje What do you mean default constructor? The phaselistener is invoked, the problem is that the '@PostConstruct' is not being called. How would an explicit default constructor help in this case? – maress Feb 03 '15 at 09:38
  • I see it is already answered. It was just a guess, since I've noticed before (not sure anymore where and when) that a missing constructor prevented another annotated method to work correctly. Luckily this does not seem the case now – Kukeltje Feb 03 '15 at 17:49

1 Answers1

3

According to section 5.4.1 of the JSF 2.2 spec, PhaseListener is not a managed bean but is injectable.

According to section 5.4.2, managed beans must support lifecycle annotations @PostConstruct and @PreDestroy.

Since a PhaseListener is not a managed bean in the sense of JSF, it does not follow from the spec that a phase listener implementation must support @PostConstruct.

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
  • Even though i accept this as the answer, it seems way weird that dependency injection is supported without life-cycle event support. – maress Apr 02 '15 at 14:48