Page Contents:Manually Creating and Destroying BeansSwiz 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: 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: 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.
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: 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 removed from the stage, or a module is unloaded. SwizConfig OptionsSix 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.
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.
Swiz and Flex Life Cycle Steps
The following table shows the steps that Flex and Swiz will go through when a new display object is set up:
The following table shows the steps that Flex and Swiz will go through when a display object is torn down:
|
Bean Life Cycle Management
Labels:
None

5 Comments
comments.show.hideJun 20, 2010
Anonymous
RE> 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.
This bit is very important to understanding. Developers may need to use [PreDestroy] which will be called after removedFromStage.
Nov 01, 2010
Anonymous
How can I determine, that the Swiz initialized? For example, in my test case during setUp?
Nov 01, 2010
Brian Kotek
If you have a specific question, the mailing list would probably provide a better answer. But in general, I use the UIImpersonator in FlexUnit 4, since it handles making sure that the container being tested has been set up.
Mar 09, 2011
Matthew Hall
Flex will sometimes dispatch a REMOVED_FROM_STAGE event immediately followed by an ADDED_TO_STAGE event when the window is resized. Does Swiz optimize this, or does this always result in a view teardown + setup cycle?
See http://blogs.adobe.com/pmartin/2010/03/use_of_stage_ev.html
Mar 09, 2011
Matthew Hall
Nevermind, answered my own question: yes, Swiz does optimize away these superfluous events. In case anyone's interested this is handled by BeanFactory methods tearDownEventHandler and addRemovedDisplayObject.