Let's face it. AS3 needed an IoC container. Spring Actionscript delivered it. In an older post, I compared
Spring Actionscript and Parsley as IoC containers and the main thing missing from Spring Actionscript (that Parsley had) was documentation. Now, with the release of Spring Actionscript 0.8, that issue is solved.
The documentation available at Spring Actionscript's site (link above) is awesome. It is in keeping with the main Spring IoC container's documentation, as well as other large libraries such as Hibernate's documentation. The documentation is pretty extensive and the feature list is growing by the day. It's becoming easier and easier to throw together a project with Spring Actionscript.
One of the new features that I had originally requested for a while back is autowiring. If you're not familiar with autowiring, it is something that will ultimately save you a lot of lines of configuration code and a lot of time. Autowiring is the process by which an IoC container finds and injects dependencies automatically, either by type or by name. Rather than writing something like this:
<object name="myObject" class="com.something.else.SuperDependency">
<property name="megaDependency">
<object class="com.something.else.MegaDependency"/>
</property>
</object>
You can write something like this:
<object class="com.something.else.SuperDependency" autowire="byType"/>
<object class="com.something.else.MegaDependency"/>
In this case, "MegaDependency" is automagically injected into "SuperDependency" because of a few things: 1) The object tag describing "SuperDependency" has the autowire attribute set to "byType", 2) "SuperDependency" declares a property "megaDependency" of type "MegaDependency". Basically, what is happening behind the scenes is that Spring Actionscript is using introspection and runtime reflection (provided by the
AS3 Commons Reflection library) to analyze the fields (getters/setters/instance level variables) of a type, and then is looking for other objects managed by the IoC (Inversion of Control) container which match that type. If it finds an object eligible for injection, it automagically injects that object into the autowired one, saving you 2-3 lines of code per object.
Granted, the process above describes autowiring
by type. There is another method of autowiring known as autowiring
by name, but that seems pretty self-explanatory, right? Find an object in the IoC container whose name matches the name of the referenced property. Eg: if "SuperDependency" has a property called "megaDependency," the IoC container will look for an object within itself called "megaDependency."
Another game-changing feature of Spring Actionscript 0.8 is Stage autowiring. When used in Flex, Spring Actionscript can now autowire stage objects. By listening for the Event.ADDED event on the Stage, Spring can tell when display objects are added to the stage. When this happens, certain properties can be autowired.
Check out this example:
...
[Autowired(mode = "byType")]
public var myDependency:MyDependency;
[Autowired(mode = "byType")]
public function get myOtherDependency():AnotherDependency { ... }
public function set myOtherDependency(value:AnotherDependency):void { ... }
Assuming this is a class that extends flash.display.DisplayObject, when this instance is added to the display list, Spring Actionscript is notified by the Event.ADDED event, and it attempts to autowire this instance. By looking at the metadata, Spring Actionscript will autowire any necessary properties declared by your class, saving you a lot of work.
Basically, this version of Spring Actionscript is amazing. I love it. By working with it, I have saved tons of time, both in development and in preventing epic madness in the future.
The Spring Actionscript team has also been extremely helpful in fixing a few bugs related to the release. The first major one of these was Module autowiring on the stage. The problem with using Modules in Flex with Spring Actionscript is that Spring is counting on all class definitions being in the main ApplicationDomain. When loading a Module, by default a new ApplicationDomain is created for memory purposes, and the Module's classes are loaded into it rather than the main ApplicationDomain.
Unfortunately this poses a problem as Spring is unable to find class definitions for all Module classes. However, the Spring Actionscript team has been very helpful in addressing the bug and trying to provide a fix. At this point, in the trunk, the issue has been patched but not fixed by preventing Modules from being autowired. Hopefully in the future, a fix will come so that we'll be able to use stage autowiring anywhere, no matter which ApplicationDomain the classes are loaded into.
Another issue that has been fixed
since the 0.8 release is complex property resolution. Spring Actionscript uses the Java property format (my.complex.property = something), and that format usually allows for complex property resolution (my.complex.property = ${my.other.property}/tk), but this wasn't working with the 0.8 release. Fortunately, I have been told that this functionality is working great in the trunk, but I have yet to try it out.
Again, mad props to the Spring Actionscript team! Thank you so much for an awesome IoC container for AS3! Looking forward to the next release!
Technorati Tags:
as3,
ioc,
dependency injection,
flash,
flex,
spring