PDA

View Full Version : having trouble getting Autowired to work with stage components in 0.8



hendershot
Jul 28th, 2009, 12:13 PM
Need some help getting Autowired to work. I am fairly new to Flex, been playing with it for a couple of months and now starting a new project and would like to use Spring DI for it.

I have custom mxml component that extends form and I am trying to inject a CairngormEventDispatcher into the component. So what I have in the form is:


[Autowired(mode='byType')]
public var cairngormDispatcher:CairngormEventDispatcher;


In my app context I have:


<object
id="cairngormDispatcher"
class="com.adobe.cairngorm.control.CairngormEventDispatch er"
factory-method="getInstance"/>

I have my project setup in flexmojos and eclipse builder, in flexmojos I've added:

<configuration>
<keepAs3Metadatas>
<keepAs3Metadata>Autowired</keepAs3Metadata>
<keepAs3Metadata>Bindable</keepAs3Metadata>
<keepAs3Metadata>Events</keepAs3Metadata>
</keepAs3Metadatas>
</configuration>

which results in: -compiler.keep-as3-metadata Autowired Bindable Events
in the compile output of flexmojos.

And in Eclipse builder I've also added: -keep-as3-metadata "Autowired"

I've tried building the application both ways and neither one does the Autowiring.

I know the app context is loaded correctly, I can do a getObject with "cairngormDispatcher" and it works. I am loading the app context on the applicationComplete event, but also have tried it with creationComplete and preinitialize.

Also I did a new FlexStageDefaultObjectSelector().approve on the custom component to verify that it would be considered for Autowiring.

So what could be wrong here? Is the compiler somehow just not picking up the
Autowired metadata? How can I investigate this further? Thanks in advance for any tips.

666shooter
Jul 29th, 2009, 04:50 AM
first of all I'd suggest in Eclipse to change your compiler argument to -keep-as3-metadata += Autowired, otherwise the other metadata's will be removed.
So far I can't really say what you're doing wrong, everything looks in order. Is it possible for you to post a small flex project here in which you only add this functionality?
Perhaps judging from that I can figure out where things go wrong.

Oh, and on a sidenote, you don't really need to inject the CairngormEventDispatcher, if you use the latest version of Cairngorm (I think its 2.2.1 or something) the CairngormEvents are 'self-dispatching'.

So instead of writing this:


CairngormEventDispatcher.getInstance().dispatchEve nt(new MyCairngormEvent());


you write this:


new MyCairngormEvent().dispatch();


That saves some typing :)

cheers,

Roland

hendershot
Jul 29th, 2009, 01:16 PM
Found the problem. Was using XMLApplicationContext instead of FlexXMLApplicationContext.

Thanks for the tip with the self dispatching events.

666shooter
Jul 30th, 2009, 02:49 AM
Hey there,

good to hear things worked themselves out for you :) If you run into anything else, feel free to ask away.

cheers,

Roland

hendershot
Jul 30th, 2009, 11:47 AM
The interesting thing now is that I don't need "-keep-as3-metadata" in eclipse or in flexmojos anymore autowiring with the metadata just works.

666shooter
Jul 30th, 2009, 12:12 PM
That is very weird, even after a clean build?

hendershot
Jul 30th, 2009, 12:51 PM
That is very weird, even after a clean build?

Yea, just tried it again, and it's autowiring.

rfkrocktk
Jul 31st, 2009, 03:01 PM
I've experienced the same thing. I'm building on 4.0.0.7219, so maybe they've changed the compiler to automagically include all metadata by default. Check out your {FLEX_HOME}/frameworks/flex-config.xml file. If you see the <keep-as3-metadata> tag commented out, you're seeing what I'm seeing.

Even though it's commented out, everything is working fine. It's pretty weird :)