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 2 Next »

[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.

  • No labels