Christophe Spring Actionscript Flex Java Exchange


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Christophe Spring Actionscript Flex Java Exchange

  1. 1. Spring ActionScript<br />ChristopheHerreman<br />
  2. 2. Agenda<br />What is Spring ActionScript<br />The IoC container<br />Configuration<br />MVC Architecture<br />Demo<br />Q&A<br />
  3. 3. What is Spring ActionScript(1/2)<br />Inversion of Control (IoC) forActionScript 3.0<br />Flash/Flex/AIR/Pure AS3<br />IoC container<br />Tailoredarchitectures (noprescriptivestructure)<br />
  4. 4. What is Spring ActionScript(2/2)<br />Started as anin-houselibrary<br />Formerlyknown as the Pranaframework<br />Incubated as a Spring Extension project<br />Upcoming release 1.0 (1.0RC1)<br />AS3Commons: Lang, Logging, Reflect, …<br />
  5. 5. The IoC Container (1/2)<br />= Object Factory<br />Creates and assembles objects<br />Centralizeddependency management<br />Spring AS: configuration via XML or MXML<br />
  6. 6. The IoC Container (2/2)<br />Object (bean): object managed and/orcreatedby the container<br />Object Factory: factorythatcreates and managesobjects<br />factory.getObject(“myObject”)<br />Object Definition: blueprintforan object<br />Application Context: smarter Object Factory<br />
  7. 7. Configuration(1/9)<br />MXML Configuration<br />// AppContext.mxml file, compiledinto the application<br /><Objects><br /> <app:ApplicationModelid=“appModel” /><br /> <app:ApplicationControllerid=“appController” applicationModel=“{appModel}”/><br /></Objects><br />
  8. 8. Configuration(2/9)<br />MXML Configuration: loading<br />var context:MXMLApplicationContext = newMXMLApplicationContext();<br />context.addConfig(AppContext);<br />context.addEventListener(Event.COMPLETE, context_completeHandler);<br />context.load();<br />function context_completeHandler(event:Event):void {<br />varappModel:ApplicationModel = context.getObject(“appModel”);<br />varappController:ApplicationController = context.getObject(“appController”);<br />}<br />
  9. 9. Configuration(3/9)<br />MXML Configuration: alternativeapproach<br />// AppContext.mxml file, compiledinto the application<br /><Objects><br /> <Object id=“appModel” clazz=“{ApplicationModel}”/><br /> <Object id=“appController” clazz=“{ApplicationController}”><br /> <Property name=“applicationModel” ref=“appModel”/><br /> </Object><br /></objects><br />
  10. 10. Configuration(4/9)<br />MXML Configuration: pros<br />Editor support<br />Simple to use<br />
  11. 11. Configuration(5/9)<br />MXML Configuration: cons<br />Compiledinto the application<br />Explicitdeclaration: limited to singletons, noexternalreferences, no prototypes -> use Spring AS MXML dialect<br />
  12. 12. Configuration(6/9)<br />XML Configuration<br />// externalcontext.xml, loadedonapplicationstartup<br /><objects><br /> <object id=“appModel” class=“”/><br /> <object id=“appController” class=“”><br /> <property name=“applicationModel” ref=“appModel”/><br /> </object><br /></objects><br />
  13. 13. Configuration(7/9)<br />XML Configuration: loading<br />var context:XMLApplicationContext = newXMLApplicationContext();<br />context.addConfigLocation(“context.xml”);<br />context.addEventListener(Event.COMPLETE, context_completeHandler);<br />context.load();<br />function context_completeHandler(event:Event):void {<br /> var appModel:ApplicationModel = context.getObject(“appModel”);<br /> var appController:ApplicationController = context.getObject(“appController”);<br />}<br />
  14. 14. Configuration(8/9)<br />XML Configuration: pros<br />Richer dialect than MXML config<br />Usablefornon-Flexprojects<br />No need to recompile (in some cases)<br />Familiar to Spring Java users<br />
  15. 15. Configuration(9/9)<br />XML Configuration: cons<br />Classes need to becompiledinto the app, noclassloading (!)<br />No editor support, only XSD<br />
  16. 16. MVC Architecture(1/3)<br />No realprescriptivearchitecture<br />Do youreallyneedone?<br />Rollyourownbecauseone does NOT fit all<br />Many MVC architectures out there:<br />Cairngorm (2), PureMVC, Mate, …<br />… Spring AS wants to support them (seeextensions)<br />
  17. 17. MVC Architecture(2/3)<br />Spring AS MVC ingredients<br />Operation API <br />Event Bus<br />Autowiring<br />Recommendations<br />Presentation Model (Fowler)<br />LayeredArchitecture (DDD – Evans)<br />
  18. 18. MVC Architecture(3/3)<br />LayeredArchitecture (DDD – Evans)<br />
  19. 19. The Operation API (1/7)<br />Asynchronousbehavior in Flash Player: loadingexternal resources, RMI, sqlite, Webservices, HTTPServices, …<br />No threading API<br />Many different APIs: AsyncToken, Responders, Callbacks, Events… we need a unifiedapproach!<br />
  20. 20. The Operation API (2/7)<br />Problem: a user repository (or service)<br />interface IUserRepository {<br />functiongetUser(id:int): ???<br />}<br />What does the getUsermethod return? AsyncToken (Flex/RemoteObject), User (In memory), void (events)<br />
  21. 21. The Operation API (3/7)<br />Spring AS solution: return anIOperation<br />interface IUserRepository {<br />functiongetUser(id:int):IOperation;<br />}<br />Cannowbeused in anyActionScript 3 context and easilymockedfortesting<br />
  22. 22. The Operation API (4/7)<br />In Spring AS, an “operation” is used to indicateanasynchronousexecution<br />IOperation interface with “complete” and “error” events<br />IProgressOperationwith “progress” event<br />OperationQueuebundlesoperations (a compositeoperation)<br />
  23. 23. The Operation API (5/7)<br />Defininganoperation:<br />public classGetUserOperationextendsAbstractOperation {<br />functionGetUserOperation(remoteObject:RemoteObject, id:String) {<br />vartoken:AsyncToken = remoteObject.getUser(id);<br />token.addResponder(newResponder(resultHandler, faultHandler));<br /> }<br /> private functionresultHandler(event:ResultEvent):void {<br />dispatchCompleteEvent(User(event.result));<br /> }<br /> private functionfaultHandler(event:FaultEvent):void {<br />dispatchErrorEvent(event.fault);<br /> }<br />}<br />
  24. 24. The Operation API (6/7)<br />Usage:<br />varoperation:IOperation = newGetUserOperation(remoteObject, 13);<br />operation.addCompleteListener(function(event:OperationEvent):void {<br />var user:User = event.result;<br />});<br />operation.addErrorListener(function(event:OperationEvent):void {<br />, “Error”);<br />});<br />
  25. 25. The Operation API (7/7)<br />A progressoperation:<br />varoperation:IOperation = newSomeProgressOperation(param1, param2);<br />operation.addCompleteListener(function(event:OperationEvent):void {<br />});<br />operation.addErrorListener(function(event:OperationEvent):void {<br />});<br />operation.addProgressListener(function(event:OperationEvent):void {<br /> var op:IProgressOperation = IProgressOperation(event.operation);<br />trace(“Progress:” + op.progress + “, total: “ +;<br />});<br />
  26. 26. The Event Bus (1/4)<br />Publish/subscribeevent system<br />Usedforglobalapplicationevents<br />Promotesloosecoupling<br />Works withstandard Flash Events; noneed to subclass Spring AS base classes<br />
  27. 27. The Event Bus (2/4)<br />Listening/subscribing to events:<br />// listen for all events<br />EventBus.addListener(listener:IEventBusListener);<br />functiononEvent(event:Event):void {<br />}<br />// listen forspecificevents<br />EventBus.addEventListener(“anEvent”, handler);<br />functionhandler(event:Event):void {<br />}<br />
  28. 28. The Event Bus (3/4)<br />Dispatching/publishingevents:<br />// dispatchstandardevent<br />EventBus.dispatchEvent(newEvent(“someEvent”));<br />// dispatchcustomevent<br />classUserEventextendsEvent {<br /> ... <br />}<br />EventBus.dispatchEvent(newUserEvent(UserEvent.DELETE, user));<br />
  29. 29. The Event Bus (4/4)<br />Routing events:<br /><object id="routeEventsProcessor" class="org.springextensions.actionscript.ioc.factory.config.RouteEventsMetaDataPostProcessor"/><br />[RouteEvents]<br />Public classMyClass {}<br />or…<br />[RouteEvents(events=“eventA,eventB”)]<br />Public classMyClass {}<br />
  30. 30. Autowiring(1/2)<br />Auto DependencyInjection via metadata<br />Autowireby type, name, constructor, autodetect<br />Works forobjectsmanagedby the container and for view components<br />Be careful: magic happens, noexplicitconfiguration<br />
  31. 31. Autowiring(2/2)<br />Annotate a propertywith [Autowired]<br />classUserController {<br /> [Autowired]<br /> public var userRepository:IUserRepository;<br />}<br />[Autowired(name=“myObject“, property=“prop”)]<br />
  32. 32. The Container (1/5)<br />Object references<br /><object id=“me” class=“Person”/><br /><object id=“john” class=“Person”><br /> <property name=“buddy" ref=“me“ /><br /></object><br />
  33. 33. The Container (2/5)<br />PropertyPlaceholderConfigurerer<br />Loadsexternalproperties<br />Properties: (<br />host=<br />port=8081<br />contex-root=myapp<br />Config:<br /><object class=“org.springextensions.actionscript.ioc.factory.config.PropertyPlaceholderConfigurer”><br /> <property name=“location” value=“”/><br /></object><br /><object id=“userRemoteObject” class=“mx.rpc.remoting.mxml.RemoteObject”><br /> <property name="endpoint" value="http://${host}:${port}/${context- root}/messagebroker/amf" /><br /></object><br />
  34. 34. The Container (3/5)<br />Parentdefinitions / abstract definitions<br /><object id="remoteObject" class="mx.rpc.remoting.mxml.RemoteObject" abstract="true"><br /> <property name="endpoint" value="http://${host}:${port}/${context-root}/messagebroker/amf" /><br /> <property name="showBusyCursor" value="true" /><br /></object><br /><object id=“userRemoteObject" parent="remoteObject"><br /> <property name="destination" value=“userService" /><br /></object><br />
  35. 35. The Container (4/5)<br />Object definition scopes<br />Singleton (default): sameinstance is returnedoneachrequest<br />Prototype: newinstance is returned<br /><object id=“mySingleton” class=“MyClass”/><br /><object id=“myPrototype” class=“MyClass” scope=“prototype”/><br />or…<br /><object id=“myPrototype” class=“MyClass” singleton=“false”/><br />
  36. 36. The Container (5/5)<br />Lazyinitializedobjects<br />Singletonsthat are notinstantiatedwhen the container starts<br /><object id=“myLazyObject” class=“MyClass”<br />lazy-init=“true”/><br />
  37. 37. And there is more…<br />Module support<br />XML namespaces (rpc, messaging, util, …)<br />Task API<br />Service base classes<br />Container extensionpoints (factory post processors, object post processors, custom metadata handlers, …)<br />Workingon: testing support, AOP, …<br />
  38. 38. Summary<br />Dependency Management<br />Loosecoupling, type to interfaces<br />Usewithotherframeworks<br />Spring mentality: provide choice<br />Promote best practices<br />
  39. 39. DEMO<br />A sample application<br />
  40. 40. Thanksforyourattention!<br /><br /><br /><br /><br />