@ PreDestroy public void destroyBeansOnPreDestroy ( ) { Map < String , ContextualStorage > oldWindowContextStorages = forceNewStorage ( ) ; if ( ! oldWindowContextStorages . isEmpty ( ) ) { FacesContext facesContext = FacesContext . getCurrentInstance ( ) ; ServletContext servletContext = null ; if ( facesContext == null ) { try { servletContext = applicationContextBean . getServletContext ( ) ; } catch ( Throwable e ) { Logger . getLogger ( FlowScopeBeanHolder . class . getName ( ) ) . log ( Level . WARNING , "Cannot locate servletContext to create FacesContext on @PreDestroy flow scope beans. " + "The beans will be destroyed without active FacesContext instance." ) ; servletContext = null ; } } if ( facesContext == null && servletContext != null ) { try { ExternalContext externalContext = new StartupServletExternalContextImpl ( servletContext , false ) ; ExceptionHandler exceptionHandler = new ExceptionHandlerImpl ( ) ; facesContext = new StartupFacesContextImpl ( externalContext , ( ReleaseableExternalContext ) externalContext , exceptionHandler , false ) ; for ( ContextualStorage contextualStorage : oldWindowContextStorages . values ( ) ) { FlowScopedContextImpl . destroyAllActive ( contextualStorage ) ; } } finally { facesContext . release ( ) ; } } else { for ( ContextualStorage contextualStorage : oldWindowContextStorages . values ( ) ) { FlowScopedContextImpl . destroyAllActive ( contextualStorage ) ; } } } }
See description on ViewScopeBeanHolder for details about how this works