// In this case, we create the application object and we destroy it when the XPages application is destroyed
FacesContextEx facesContext = FacesContextEx.getCurrentInstance();
if(facesContext!=null) {
final XspApplication newApp = app = (XspApplication)initApplication(facesContext.getExternalContext().getContext());
ApplicationEx facesApplication = facesContext.getApplicationEx();
facesApplication.addApplicationListener(new ApplicationListener() {
public void applicationDestroyed(ApplicationEx application) {
}
public void applicationCreated(ApplicationEx application) {
destroyApplication(newApp);
}