Bean Life Cycle Management

Learn about the bean life cycle

Page Contents:

Manually Creating and Destroying Beans

Crux provides an event-based mechanism to create or destroy beans. To create a new bean, you can dispatch the BeanEvent.SET_UP_BEAN or BeanEvent.ADD_BEAN events:

[Dispatcher]
public var dispatcher : IEventDispatcher;
 
private function createNewBean() : void
{
    var userModel : UserModel = new UserModel();
     
    // Crux will create a bean for the userModel, and process any metadata in it.
    dispatcher.dispatchEvent( new BeanEvent( BeanEvent.SET_UP_BEAN, userModel ) );
}

Both SET_UP_BEAN and ADD_BEAN will process the target object as a bean. The difference between these events is that ADD_BEAN will also add the bean as a singleton to the BeanFactory cache, while SET_UP_BEAN does not add the new bean to the cache.

Similarly, to destroy a bean, you dispach the BeanEvent.TEAR_DOWN_BEAN or BeanEvent.REMOVE_BEAN events:

private function destroyBean() : void
{
    // Crux will destroy the userModel bean, clean up any injected objects, and delete any injection bindings.
    dispatcher.dispatchEvent( new BeanEvent( BeanEvent.TEAR_DOWN_BEAN, userModel ) );
}

The tear down events mirror the set up events discussed previously: TEAR_DOWN_BEAN cleans up the target by removing injections, event handlers, etc., while REMOVE_BEAN cleans up the bean as well as removing it from the singleton cache in the BeanFactory.

Since BeanEvent is a bubbling event, you can dispatch it from a view. If you dispatch them from non-view beans, be sure you use an injected dispatcher so that Crux can handle the event.

If necessary, you can directly call the setUpBean() and tearDownBean() methods on the BeanFactory. Since these methods both take a Bean instance as an argument, you can use the createBeanForSource() method on the BeanFactory to generate a Bean instance that you can then pass into the set up and tear down methods. However, in general the event-based approach to creating and tearing down beans should be the preferred approach.

[PostConstruct] and [PreDestroy]

Crux provides two metadata tags which allow you to trigger methods when any bean is set up or torn down. You can decorate a public method with [PostConstruct] and that method will be invoked by the framework after the bean has been set up, had dependencies injected, and had mediators created. For example:

package org.apache.royale.quickcrux.controller
{
    import org.apache.royale.quickcrux.service.UserService;
 
    public class UserController
    {
        [Inject]
        public var userService : UserService;
 
        /**
         * [PostConstruct] methods are invoked after all dependencies are injected.
         */
        [PostConstruct]
        public function createDefaultUser() : void
        {
            userService.loadDefaultUser();
        }
    }
}

Similarly, a public method decorated with [PreDestroy] will be called when a bean is destroyed by Crux. This would happen if a UI component is removed from the stage, or a module is unloaded.

package org.apache.royale.quickcrux.controller
{
    import org.apache.royale.quickcrux.service.UserService;
 
    public class UserController
    {
        [Inject]
        public var userService : UserService;
 
        /**
         * [PreDestroy] methods are invoked when a bean is destroyed.
         */
        [PreDestroy]
        public function clearPollingTimer() : void
        {
            userService.stopPolling();
        }
    }
}