Exemple avec JSF 2 PostConstructApplicationEvent et PreDestroyApplicationEvent

Exemple JSF 2 PostConstructApplicationEvent et PreDestroyApplicationEvent

Depuis JSF 2.0, vous pouvez enregistrer l'événement systèmejavax.faces.event.PostConstructApplicationEvent etjavax.faces.event.PreDestroyApplicationEvent pour manipuler le cycle de vie de l'application JSF.

1. PostConstructApplicationEvent - Effectuez une post-configuration personnalisée après le démarrage de l'application.
2. PreDestroyApplicationEvent - Effectuez une tâche de nettoyage personnalisée avant que l'application ne soit sur le point d'être fermée.

Note
Dans JSF, vous ne pouvez pas dépendre duServletContextListeners standard pour effectuer la tâche ci-dessus, car lesServletContextListeners peuvent être exécutés avant le démarrage de l'application JSF.

L'exemple suivant vous montre comment créer un événement systèmePostConstructApplicationEvent etPreDestroyApplicationEvent dans JSF 2.0.

1. Implémente SystemEventListener

Créez une classe qui implémentejavax.faces.event.SystemEventListener et remplacez les méthodesprocessEvent() etisListenerForSource() pour votre tâche de post-configuration et de nettoyage personnalisée.

package com.example;

import javax.faces.application.Application;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.PostConstructApplicationEvent;
import javax.faces.event.PreDestroyApplicationEvent;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;

public class FacesAppListener implements SystemEventListener{

  @Override
  public void processEvent(SystemEvent event) throws AbortProcessingException {

    if(event instanceof PostConstructApplicationEvent){
        System.out.println("PostConstructApplicationEvent is Called");
    }

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

  }

  @Override
  public boolean isListenerForSource(Object source) {
    //only for Application
    return (source instanceof Application);

  }

}

2. Enregistrer les événements système

Enregistrez les événements systèmePostConstructApplicationEvent etPreDestroyApplicationEvent dans le fichierfaces-config.xml comme ceci:

faces-config.xml



    

        
        
        
            com.example.FacesAppListener
        
        
            javax.faces.event.PostConstructApplicationEvent
        
        

        
        
        
            com.example.FacesAppListener
        
        
            javax.faces.event.PreDestroyApplicationEvent
        
        

    

3. Demo

Exécutez votre application JSF. La méthodeprocessEvent() est exécutée après le démarrage de votre application JSF, voir la figure ci-dessous:

jsf2-PostConstructApplicationEvent-example

Note
Cependant, lePreDestroyApplicationEvent n'est pas vraiment fiable, car JSF ne l'exécutera pas s'il est arrêté anormalement. Par exemple, un processus Java tué par l'administrateur système, c'est toujours arrivé :). Veuillez donc utiliser cet événement système à bon escient.

Télécharger le code source