JSF initialize application-scope bean when context initialized

前端 未结 3 1858
忘掉有多难
忘掉有多难 2020-12-15 06:55

I\'m building a JSF+Facelets web app, one piece of which is a method that scans a directory every so often and indexes any changes. This method is part of a bean which is i

3条回答
  •  轮回少年
    2020-12-15 07:50

    In JSF 2+ you can use a SystemEventListener to handle it. You would set it to take action on the PostConstructApplicationEvent to initialize it.

    
        
         listeners.SystemEventListenerImpl
        
        
         javax.faces.event.PostConstructApplicationEvent
                               
    
    

    The implementation would look something like :

    public class SystemEventListenerImpl implements SystemEventListener {
    
      @Override
      public void processEvent(SystemEvent event) throws AbortProcessingException {
        Application application = (Application) event.getSource();
       //TODO
      }
    
      @Override
      public boolean isListenerForSource(Object source) {
        return (source instanceof Application);
      }
    }
    

    This will allow you to do a lot more than just simply passing a value.

提交回复
热议问题