简体   繁体   中英

@PostConstruct not invoked in PhaseListener

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM