Spring Actionscript 是衆多圍繞依賴注入提供解決方案的Flex控制反轉框架之一
html
AS3 下經典的IOC框架有Spring ActionScript、Parsley、Flicc和Swiz,因爲我對JAVA spring IOC機制較爲熟悉,因此選擇了 Spring ActionScript。
java
主要有XML配置(XML)和註釋元數據(Metadata)配置兩種方式,下面一一介紹,例子是基於spring AS 2.0.1版本展開的。--流子spring
1.從Spring Actionscript 主頁下載依賴包 app
spring-actionscript-core-X.X.X.swc
框架
spring-actionscript-flex-X.X.X.swc
函數
另有as3commons依賴包須要下載。建議下載這個 flex
Spring Actionscript X.X.X Full (with dependencies and Flex extensions)ui
2.1 XML配置方式this
配置文件application-context.xmlspa
<?xml version="1.0" encoding="UTF-8"?> <objects xmlns="http://www.springactionscript.org/schema/objects" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springactionscript.org/schema/objects http://www.springactionscript.org/schema/objects/spring-actionscript-objects-2.0.xsd"> <object id="ConfigManager" class="com.yimi.fairy.client.ConfigManager" init-method="initialize"> <!--<constructor-arg ref="authenticationService"/> 構造函數時引入 --> </object> <object id="RoleManager" class="com.yimi.fairy.client.service.role.RoleManager" init-method="initialize"> <property name="configManager" ref="ConfigManager" /> </object> <object id="ResourceManager" class="com.yimi.fairy.client.res.ResourceManager"></object> <object id="JobManager" class="com.yimi.fairy.client.job.JobManager"></object> <object id="RoleDataTemplateManager" class="com.service.role.RoleDataTemplateManager"> <constructor-arg ref="ResourceManager"/> </object> </objects>
程序載入:
var context:XMLApplicationContext=new XMLApplicationContext("application-context.xml"); context.addEventListener(Event.COMPLETE, this.handleComplete); context.load();
對容器中對象讀取:
var configManager:ConfigManager=context.getObject("ConfigManager");
2.2 註釋元數據(Metadata)配置方式
優勢:把application-context.xml 徹底幹掉,徹底經過註釋方式實現。它是對加載進來的SWF文件進行類掃描把[Component] 或者[Configuration] 讀取並解析進IOC容器。
var rootViews:Vector.<DisplayObject> = new <DisplayObject>[display]; var context:MetadataApplicationContext= new MetadataApplicationContext(rootViews); context.addEventListener(Event.COMPLETE, this.handleComplete); context.load();對象配置:
[Component(id="roleDataTemplateManager",initMethod="initialize")] public class RoleDataTemplateManager implements IResourceLoader { [Autowired] public var resourceManager:ResourceManager; public function initialize():void { resourceManager.register(this); } }注意注入進去的對象必須是public,不然注不進去。
var configManager:ConfigManager=context.getObject("configManager");2.3 如何注入第三方庫中的對象
[Configuration] public final class AppConfig { public var stageAutowireProcessor:DefaultAutowiringStageProcessor; public var eventHandlerMetadataProcessor:EventHandlerMetadataProcessor; public var routeEventsMetadataProcessor:RouteEventsMetaDataProcessor; }
原文連接:http://blog.csdn.net/jiangguilong2000/article/details/9705479