The DisposableBean Interface

Managed beans, particularly session and request scoped beans, can have references to resources that may need to be specifically dealt with when the beans go out of scope. Therefore, it can be useful to know when this occurs. There is nothing in the current JSF API to help with this and, if you can't use annotations or some other injection solution for your beans, then it can be difficult to address the clean up of these resources.

To help with this, the ICEfaces framework provides an interface called com.icesoft.faces.context.DisposableBean.

Note: The previously recommended solution for this was the com.icesoft.faces.context.ViewListener interface. The ViewListener interface is now deprecated in favor of the DisposableBean interface. While ViewListener could be applied more broadly (i.e., to non-bean classes), it was more difficult to use and worked in more restricted situations.

The interface provides a single method:

void dispose() throws Exception;

To use it, implement the DisposableBean interface on your managed bean and then in the dispose() method, do any clean up necessary when the bean goes out of scope. Here is an very simple example of a managed bean that implements the DisposableBean interface:

package com.icesoft.faces.example;

import com.icesoft.faces.context.DisposableBean;

public class UserBean implements DisposableBean {

    public UserBean() {
        //resources create or acquired

    public void dispose() throws Exception {
        //resources cleaned up
Note: Currently, the DisposableBean works only if the user leaves the application entirely, or if the user navigates within the application by clicking an anchor tag or by redirection. This mechanism does not work if the user submits a postback and the view is changed by evaluating a navigation rule.

For further examples using DisposableBean interface, check out the ICEfaces Tutorial code and the Auction Monitor sample application. The DisposableBean interface is frequently used in conjunction with the Renderable interface when doing AJAX push. For more information about AJAX push, see Server-initiated Rendering API.

Copyright 2005-2008. ICEsoft Technologies, Inc.