Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 26 Next »

Manually Creating and Destroying Beans

Swiz 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:

Unknown macro: {html}

<script src="http://gist.github.com/396855.js?file=swiz_setupbean.java"></script>

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:

Unknown macro: {html}

<script src="http://gist.github.com/396855.js?file=swiz_teardownbean.java"></script>

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 Swiz 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]

Swiz 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:

Unknown macro: {html}

<script src="http://gist.github.com/396855.js?file=swiz_postconstruct.java"></script>


Similarly, a public method decorated with [PreDestroy] will be called when a bean is destroyed by Swiz. This would happen if a UI component is garbage collected, or a module is unloaded.

Unknown macro: {html}

<script src="http://gist.github.com/396855.js?file=swiz_predestroy.java"></script>


SwizConfig Options

Six configuration options are available in the SwizConfig object to specify how UI components are handled by the framework. These are setUpEventType, setUpEventPhase, setUpEventPriority, and the corresponding tearDownEventType, tearDownEventPhase, and tearDownEventPriority. Normally, you can leave these at their default values. But if you need to, you can modify these to alter how Swiz creates and destroys beans that are UI components.

The default setUpEventType is "addedToStage". This means that whenever a UI component is added to the stage, Swiz will inspect the component and process any metadata it finds. Any dependency injections and event mediators will happen at this time. As mentioned, you may change this value if "addedToStage" is not ideal for your situation. "creationComplete" is another commonly used setUpEventType.

Be careful if you use an event type that occurs early in the Flex component life cycle, such as "preinitialize", since child components have not been created yet. Conversely, be aware that the Flex framework may internally trigger the removal and addition of components at unexpected times, such as when scroll bars are added to a resized container. In the unlikely (but possible) event that this causes problems, consider an alternative setUpEventType.

At the other end of the bean life cycle, the default tearDownEventType is "removedFromStage". This means that when a UI component is removed from the stage, Swiz will perform clean up activities such as removing event mediators.

If you require even more fine-grained control, you can specify alternative values for the phase and priority used for the set up and tear down of beans. Typically, these won't need to be changed, but the options are there in case they are needed.

You can also use the ISetUpValidator and ITearDownValidator interfaces with UI components to control whether set up or tear down are allowed.


Swiz and Flex Life Cycle Steps

The following table shows the steps that Flex and Swiz will go through on application startup:

Type

Step

Flex

Preinitialize event

Swiz

dispatcher set

Swiz

Swiz created event

Swiz

domain set

Swiz

Global dispatcher set

Swiz

Processors initialized

Swiz

Bean factory initialized

Swiz

setUpEvent and tearDownEvent values set from SwizConfig

Swiz

Beans defined in the BeanProvider(s) are processed

Swiz

(per bean) beanFactory.setUpBean()

Swiz

(per bean) [Inject] processed

Swiz

(per bean) [EventHandler] processed

Swiz

(per bean) [Dispatcher] processed

Swiz

(per bean) Default custom metadata processed

Swiz

(per bean) [PostConstruct] processed

Flex

Initialize event

Flex

Creation complete event

Flex

Added to stage event

Flex

Display objects in the display list are processed (see table below)

Flex

Update complete event

Flex

Application complete event


The following table shows the steps that Flex and Swiz will go through when a new display object is set up:

Type

Step

Flex

Invalidation

Flex

Property bindings

Flex

Preinitialize

Flex

Create children

Flex

Initialize event

Flex

Commit properties

Flex

Resize

Flex

Render

Flex

Measure

Flex

Set actual size

Flex

Update display list

Flex

Creation complete event

Flex

Added event

Flex

Added to stage event

Swiz

beanFactory.setUpBean()

Swiz

[Inject] processed

Swiz

[EventHandler] processed

Swiz

[Dispatcher] processed

Swiz

Default custom metadata processed

Swiz

[PostConstruct] processed

Flex

Update complete event


The following table shows the steps that Flex and Swiz will go through when a display object is torn down:

Type

Step

Flex

Removed event

Flex

Removed from stage event

Swiz

[PreDestroy] processed

Swiz

[Inject] tear down

Swiz

[EventHandler] tear down

Swiz

[Dispatcher] tear down

Swiz

Default custom metadata tear down

  • No labels