Need Help?
For technical questions, feel free to ask on the Swiz mailing list.
Save as HTML or PDF
You can export this entire Wiki in HTML or PDF format.

Dependency Injection

Skip to end of metadata
Go to start of metadata

Page Contents:

Dependency Injection Overview

The IoC Container of Swiz is the BeanProvider class. Any beans defined in a registered BeanProvider can be injected into other registered beans or into views added to the display list.

Defining an injection target is done using the [Inject] metadata tag. Common injection scenarios include the following:

  • Inject Model into View
  • Inject Model into Controller
  • Inject Delegate into Controller
  • Inject Service into Delegate
Due to restrictions imposed by the AVM, Swiz cannot inject into private, protected or internal members.
By default, injections in view components happen during the ADDED_TO_STAGE event. This behavior can be modified by changing the Swiz configuration. It is important to understand that this means that injections occur after the CREATION_COMPLETE event. If you need to trigger logic during creation that depends on injections, use a [PostConstruct] method.


Inject by type

If you define exactly one bean of a given type you can inject it into a matching destination with a plain [Inject] tag. Note this also supports injecting a concrete implementation of an interface to which the destination is typed. In other words, if a property decorated with [Inject] is of type IUserService, Swiz will correctly inject a bean implementing that interface, such as UserService or MockUserService.


Best Practice
You should inject by type whenever possible. It avoids reliance on string based names, which can lead to run time errors caused by misspellings.
Automatic Creation and Injection of Built-In Helper Classes
If Swiz encounters an [Inject] tag for a property typed to ServiceHelper, IServiceHelper, URLRequestHelper, IURLRequestHelper, or MockDelegateHelper, and there is no matching Bean defined in a BeanProvider, Swiz will automatically create a bean of the correct type and inject it for you.


Inject by name

If you have more than one bean of a given type defined you can request that a bean with a specific name be injected. This bean name must match the id attribute of the bean's tag as defined in your BeanProvider.


Quick Tip
"source" is the default attribute of [Inject], so the above tag could also be written as [Inject( "userService" )]


Inject bean property

Swiz can also inject public bean properties, rather than just the beans themselves using dot notation. In the following example "userModel" is the name of a bean, and we are injecting its currentUser property.


If the bean property is bindable and the decorated property is public, you can have Swiz create a binding to the source property by adding bind="true" to the [Inject] tag:


As of Swiz 1.3, you can also inject nested properties, such as:


Two way bindings

To set up a reverse binding, so the bean property will receive updates made to the injection target, you can set the twoWay attribute of the [Inject] tag to true. This of course requires that the injection target is bindable as well.


Setter injection

Swiz can also inject values into single argument methods. All of the attributes and policies mentioned above apply to setter injection as well, with the exception of twoWay.


Injection destination

Sometimes it's useful to define an injection someplace other than directly on a property/method. This is especially true in MXML views, where decorating a child component's property is not possible. Rather than creating a local variable and binding your component property to it, you can simply use the destination attribute of [Inject]. If this were defined in an MXML file, it would be wrapped in a Metadata tag.


Take care to ensure that the injection destination will be created and available by the time your injections are processed. The injection processing trigger can be configured in SwizConfig, but defaults to the ADDED_TO_STAGE event.
Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. May 21, 2010

    Anonymous

    I was once aware of the difference between [Autowire] and binding.. However the difference between [Inject] with bind property to [Inject] without and the meaning of the new destination property makes it unclear to me.... This creates a mixup between what Swiz does and what the flex binding mechanism does.

    1. May 26, 2010

      If you add bind then Swiz creates a binding between the property you're injecting into and the source property (as if you set up a binding yourself using the Flex BindingUtils class). Without bind (which is the default), it doesn't create a binding. If you're not familiar with what this actually means, you may want to read through that section of the Flex documentation.

      Destination can be useful if you want to inject (and potentially bind) directly to a component property, rather than having to declare your own property, inject into it, and then bind some other component property to your own property. Basically, I haven't found much use for it so if that isn't clear, I really wouldn't worry about it. As the note above says, you have to be careful with destination because the destination may still be null (if Flex hasn't created that component yet).

  2. Aug 13, 2010

    Anonymous

    Sad, I can't get anything sample swiz project to work with Flex Builder 3, SDK 3.5.

    I tried the simplest feature of Swiz, inject a model object into the main app and into a view componenet.

    Neither of them get the model injected, they remained null.

    Can someone please help?

    Just give a sample flex 3 app that uses the latest Siwz 1.0-RC1 with no services, just model injection for now.

    Thanks,

    1. Aug 13, 2010

      If you have a question, please post it to the mailing list, that's what it's there for. Thanks.

      1. Oct 14, 2010

        Anonymous

        where is the the mailing list?

        1. Oct 17, 2010

          It's listed on the wiki home page, in the FAQ, and on the left sidebar of every page on this wiki.

  3. Oct 14, 2010

    Anonymous

    in spring. we can inject collections like(list map)

    such as below:

    <beans> 
        <bean id="someBean"> 
            <property name="someArray"> 
                <list> 
                    <value>Hello!Justin!</value> 
                    <value>Hello!Momor!</value> 
                    <value>Hello!Bush!</value> 
                </list> 
            </property> 
            <property name="someObjArray"> 
                <list> 
                    <ref bean="someObj1"/> 
                    <ref bean="someObj2"/> 
                </list> 
            </property> 
            <property name="someList"> 
                <list> 
                     <value>Hello!Justin!</value> 
                     <ref bean="someObj1"/> 
                     <ref bean="someObj2"/> 
                </list> 
            </property> 
            <property name="someMap"> 
                <map> 
                     <entry key="somekey1"> 
                         <ref bean="someObj1"/> 
                     </entry> 
                     <entry key="somekey2"> 
                         <value>Hello!Justin!</value> 
                     </entry> 
                </map> 
            </property> 
        </bean> 
    </beans>

    there are just [Inject metadata tag] in swiz and it can't inject more than two beans(Config in the BeanProvider) into an ArrayCollection property. Would you please give me some adives if I want to function.

    1. Oct 17, 2010

      Spring is configured with plain XML. Since Swiz BeanProviders are actual MXML declarations of classes, you can specify properties the same way you would in any other MXML code. So your Spring configuration code above could be used simply as:

      1. Oct 22, 2010

        Anonymous

        thanks!

        It sounds good. but how about inject the flash.utils.Dictionary(like a map)

        1. Oct 28, 2010

          You'd do it the same way, either referencing an existing Dictionary or creating a new instance when you set the property. If you have more questions, feel free to ask on the mailing list (see link on the left). We're trying to keep the wiki comments specific to the documentation. Thanks.

  4. Dec 08, 2010

    Anonymous

    For people doing inject by name, or inject bean property, beware: When defining BeanProvider inline, you have to wrap the actual instance in a
    Bean tag and use the name attribute. See http://groups.google.com/group/swiz-framework/browse_thread/thread/92a8938a9c0a9fee

    1. Dec 08, 2010

      Thanks for reminding me about that. I've updated the IoC container page to include a note about this.

  5. Dec 17, 2010

    Anonymous

    As help to others.  If you are injecting a bean property and you want it to update when the value being injected is updated, but sure to set the bind property on your inject  (e.g.  [Inject( source="userModel.currentUser", bind="true" )]   ).  This should create a ChangeWatcher to update the property for you.

  6. Jun 23, 2011

    Anonymous

    Is it planned to inject properties no in the fx:script tag like it is so far but rather in the fx:declaration?

    I'm just porting a project from Fx3 to Fx4 and wondered if that would be possible or makes sense at all? :-)

    Thx

    Matt 

    1. Jun 23, 2011

      Hi Matt. No, this won't work because there is no way to attach metadata to something declared in an MXML declarations block.

  7. Aug 04, 2011

    Anonymous

    Hi Guys,

        Please give me sample code of swiz with flex..

    1. Aug 04, 2011

      Not sure how you missed the Examples item right over there in the menu tree? http://swizframework.jira.com/wiki/display/SWIZ/Examples