Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Section
Column
width15px

Column

Page Contents:

Table of Contents
minLevel4
maxLevel4

The BeanProvider Class

The IoC (Inversion of control) container of Swiz is the BeanProvider class. In the BeanProvider you typically define non-view classes that you want registered with the framework and available as injection sources or targets. This generally means models, controllers, delegates and services. Beans are defined using regular MXML tags:

Code Block
xml
xml
<?xml version="1.0" encoding="utf-8"?>
<swiz:BeanProvider
	xmlns:swiz="http://swiz.swizframework.org"
	xmlns:model="com.example.model.*"
	xmlns:control="com.example.control.*">

	<model:ApplicationModel id="applicationModel" />
	<control:UserController id="userController" />

</swiz:BeanProvider> 


Most applications define at least one Beans.mxml in the root package and pass it to the Swiz tag's beanProviders property in the application’s main MXML file. For clarity and simplicity, it is recommended that you define your beans in a separate file, using BeanProvider as the root tag, as shown above. However, if for some reason you choose to define your beans inline in the Swiz tag, you'll need to wrap your objects with the Bean tag to ensure they are processed correctly:

Code Block
xml
xml
<swiz:Swiz>
	<swiz:beanProviders>
		<swiz:BeanProvider>
			<swiz:Bean name="applicationModel">
				<model:ApplicationModel />
			</swiz:Bean>
			<swiz:Bean name="userController">
				<control:UserController />
			</swiz:Bean>	
		</swiz:BeanProvider>
	</swiz:beanProviders>

	<swiz:config>
		<swiz:SwizConfig
			eventPackages="com.foo.events, org.bar.events"
			viewPackages="com.foo.views, org.bar.events" />
	</swiz:config>
</swiz:Swiz>


Prototype Beans

Similar to Spring’s prototype scope, Swiz provides a Prototype tag. Prototype enables a few different things:

  • Constructor injection – you can pass up to 8 arguments to your bean’s constructor
  • Unique copy for each injection target (like prototype scope behavior in Spring)
  • Deferred instantiation – Prototype beans will not be created until they are needed for injection
Code Block
xml
xml
<swiz:Prototype id="editViewPresoModel"
	type="{ EditViewPresentationModel }"
	constructorArguments="{ someOtherBean }" />


Tip

If you want deferred instantiation and/or constructor injection but not a unique copy for each injection you can set Prototype’s singleton property to true.

A

Prototype

bean

created

and

injected

into

a

view

is

*

not

*

destroyed

when

the

view

is

removed.

Swiz

cannot

know

what

else

you

may

have

done

with

the

Prototype.

If

you

wish

to

tear

down

a

Prototype

bean

when

a

view

is

removed,

implement

a

[{{\

[PreDestroy

\

]

}}

method

and

dispatch

a

{

TEAR_DOWN_BEAN

}

event

|Bean Life Cycle Management]

for

the

Prototype

bean.

Tip
Wiki Markup


Externalizing Configuration Values

Bean property values can also be defined in external XML files. The SwizConfigValueLoader Swiz extension provides one way to handle this. For example, an XML file with this structure:

Code Block
xml
xml
<config>
    <value1>This is value 1.</value1>
    <value2>This is value 2.</value2>
</config>


Can be loaded and used in a BeanProvider by doing:

Code Block
xml
xml
<externalconfig:SwizConfigValueLoader id="configLoader" source="config.xml" />
<controller:MyController id="myController"
                         value1="{configLoader.configData.config.value1}" 
                         value2="{configLoader.configData.config.value2}" />


The SwizConfigValueLoader can be injected just like any other bean:

Code Block
as3
as3
[Inject]
public var configLoader : SwizConfigValueLoader;


Finally, you can create an event handler to execute after the external data is loaded (remember to add "org.swizframework.externalconfig.event" to your eventPackages):

Code Block
as3
as3
[EventHandler( event = "ConfigLoaderEvent.CONFIG_LOAD_COMPLETE", properties = "configData" )]
public function onConfigLoadComplete( configData : Object ) : void
{
    // You can now reference the value with syntax such as: 
    // configData.config.value1, configData.config.value2, etc.
}
Column
width15%