主要的两个类:
1,FlexXMLApplicationContext,XMLApplicationContext,是IOC容器的两个重要类,FlexXMLApplicationContext只是增加了一些flex方面的特性。
2,定义文件模板:
<?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-1.0.xsd">
<object id="..." class="...">
<!-- collaborators and configuration for this object go here -->
</object>
<object id="..." class="...">
<!-- collaborators and configuration for this object go here -->
</object>
<!-- more object definitions go here -->
</objects>
3,初始化容器:
var applicationContext:XMLApplicationContext = new XMLApplicationContext();
applicationContext.addConfigLocation("application-context.xml");
applicationContext.addEventListener(Event.COMPLETE, handleComplete);
applicationContext.load();
application-context.xml路径也可以通过FlashVars来设置
4,组合xml文件定义
<import file="services.xml"/>
<import file="resources/messageSource.xml"/>
<import file="/resources/themeSource.xml"/>
5,绑定xml数据
[Bindable]
[Embed(source="application-context.xml",mimeType ="application/octet-stream")]
public var contextConfig:Class;
var applicationContext:XMLApplicationContext= new XMLApplicationContext();
applicationContext.addEmbeddedConfig(contextConfig);
applicationContext.load();
6, 绑定组合xml
[Bindable]
[Embed(source="services.xml",mimeType ="application/octet-stream")]
public var servicesContext:Class;
[Bindable]
[Embed(source="resources/messageSource.xml",mimeType ="application/octet-stream")]
public var messageSourceContext:Class;
[Bindable]
[Embed(source="/resources/themeSource.xml",mimeType ="application/octet-stream")]
public var themeSourceContext:Class;
<objects>
<import file="servicesContext" type="class"/>
<import file="messageSourceContext" type="class"/>
<import file="themeSourceContext" type="class"/>
<object id="object1" class="..."/>
<object id="object2" class="..."/>
</objects>
7,定义属性文件:
<property file="strings.properties" />
通过${...} 语法来访问。
8,释放容器管理对象
(1)实现IDisposable接口,
public interface IDisposable {
function get isDisposed():Boolean;
function dispose():void;
}
(2)在配置文件上制定destroy-method.
9, 实例化对象:
10,静态工厂实例化
<object id="exampleObject" class="examples.ExampleObjectLocator" factory-method="getInstance"/>
或者
<object id="exampleFactory" class="examples.ExampleObjectFactory"/> <object id="exampleObject" class="examples.Example" factory-object="exampleFactory" factory-method="getInstance"/>And for this the Actionscript equivalent would be:
var exampleFactory:ExampleObjectFactory = new ExampleObjectFactory(); var exampleObject:ExampleObject = exampleFactory.getInstance();
<object id="exampleObject" class="examples.ExampleObject"/> <object name="anotherExample" class="examples.ExampleObjectTwo"/>
The Actionscript equivalent of which would be:
var exampleObject:ExampleObject; var anotherExample:ExampleObjectTwo;
var exampleObject:ExampleObject = applicationContext.getObject("exampleObject") as ExampleObject;
Which in regular Actionscript would look like this:
var exampleObject:ExampleObject = new ExampleObject();