JSF注释@ListenerFor不适用于GlassFish或Tomcat。没有错误或警告。只是不调用方法processEvent()。

@ListenersFor({@ListenerFor(systemEventClass=PostConstructApplicationEvent.class),
public class MySystemEventListener implements SystemEventListener {

   @Override
   public void processEvent(SystemEvent event) throws AbortProcessingException {
      if(event instanceof PostConstructApplicationEvent){
         System.out.println("*********************************************");
         System.out.println("processEvent Method is Called: PostConstructApplicationEvent");
         System.out.println("*********************************************");
      }

      if(event instanceof PreDestroyApplicationEvent){
         System.out.println("*********************************************");
         System.out.println("processEvent Method is Called: PreDestroyApplicationEvent");
         System.out.println("*********************************************");
      }
}

   @Override
   public boolean isListenerForSource(Object o) {
      return (o instanceof Application);
   }

}

有什么主意吗?

最佳答案

正如其javadoc告诉您的那样,@ListenerFor旨在放在UIComponentRenderer实现上,而不放在独立的SystemEventListener实现上。对于后者,您需要将其注册为<system-event-listener>中的faces-config.xml

例如。

<application>
    <system-event-listener>
        <system-event-listener-class>com.example.MySystemEventListener</system-event-listener-class>
        <system-event-class>javax.faces.event.PostConstructApplicationEvent</system-event-class>
        <system-event-class>javax.faces.event.PreDestroyApplicationEvent</system-event-class>
    <system-event-listener>
</application>

对于特定的功能要求,您可能要考虑使用预先初始化的应用程序范围的Bean。这有点容易,不需要一些冗长的XML:
@ManagedBean(eager=true)
@ApplicationScoped
public void App {

    @PostConstruct
    public void init() {
        // ...
    }

    @PreDestroy
    public void destroy() {
        // ...
    }

}

10-07 22:35