BootStrap Loader

来源:互联网 发布:电脑练字软件 编辑:程序博客网 时间:2024/05/16 17:09

BootStrap Loader(引导程序)是一个轻量级的应用程序,它定义了应用程序之间想要共享的类。

BootStrap Loader第一个被加载,而在把主应用程序加载到自己的子应用程序域。然后主程序加载一个子程序到同域中。这连个程序都会被加载到bootstrap loader的应用程序域的子程序域。这样它们就可以共享任何在bootstrap里面定义的类,当然前提是使用它们自己的类定义。

通常在一个安全域中只有一个bootstrap loader。在不同的安全域中Bootstrap里面的类是不共享的。

Bootstrap Loader里面任何一个类的改变都会影响其它子程序。所以,要确保加入到BootStrap Loader里面的类一般是不经常改变的。

当你使用RPC类和Blaze DS、LCDS时,你就必须把RPC类隔离出BootStrap Loader。bootstrap loader应该只包含RPC类。你通常不添加你的自定义的值对象类到bootstrap loader来得到和设置服务器上数据,你可以添加这些类减少封装。但是如果是那样的话,这些类应该是简单的类,而不要关联到Flex框架,例如Array、ArrayCollection。添加复杂的值对象类到bootstrap loader是不支持的。

下面是一个RPC类的bootstrap loader的例子,这种bootstrap loader通常使用在子程序使用HTTPService和WebService类,以及代理服务。

注意MainApp.swf被加载,bootstrap loader使用Loader类,没有定义LoaderContext,意味着使用默认的Loader。默认是把子程序加载到一个子应用程序域,采取默认的安全域。

同时也注意每一条import,类的声明是紧接着导入的,结果就是这些类类定义直接关联到bootstrap loader中,主程序和子程序可以共享这些类了。

代码示例如下:

package {import flash.display.Loader;import flash.display.Sprite;import flash.display.StageAlign;import flash.display.StageScaleMode;import flash.events.Event;import flash.net.URLRequest;import flash.system.ApplicationDomain;/** *  Classes used by the networking protocols go here. These are the classes *  whose definitions are added to the bootstrap loader and then shared *  by the main application and all sub-applications. */import mx.messaging.config.ConfigMap; ConfigMap;import mx.messaging.messages.AcknowledgeMessage; AcknowledgeMessage;import mx.messaging.messages.AcknowledgeMessageExt; AcknowledgeMessageExt;import mx.messaging.messages.AsyncMessage; AsyncMessage;import mx.messaging.messages.AsyncMessageExt; AsyncMessageExt;import mx.messaging.messages.CommandMessage; CommandMessage;import mx.messaging.messages.CommandMessageExt; CommandMessageExt;import mx.messaging.messages.ErrorMessage; ErrorMessage;import mx.messaging.messages.HTTPRequestMessage; HTTPRequestMessage;import mx.messaging.messages.MessagePerformanceInfo; MessagePerformanceInfo;import mx.messaging.messages.RemotingMessage; RemotingMessage;import mx.messaging.messages.SOAPMessage; SOAPMessage;import mx.messaging.channels.HTTPChannel; HTTPChannel;import mx.core.mx_internal;[SWF(width="600", height="700")]public class RPCBootstrapLoader extends Sprite {        /**         *  The URL of the application SWF that this bootstrap loader loads.         */        private static const applicationURL:String = "MainApp.swf";        /**         *  Constructor.         */        public function RPCBootstrapLoader() {                super();                if (ApplicationDomain.currentDomain.hasDefinition("mx.core::UIComponent"))                        throw new Error("UIComponent should not be in the bootstrap loader.");                stage.scaleMode = StageScaleMode.NO_SCALE;                stage.align = StageAlign.TOP_LEFT;                if (!stage)                        isStageRoot = false;                root.loaderInfo.addEventListener(Event.INIT, initHandler);        }        /**         *  The Loader that loads the main application's SWF file.         */        private var loader:Loader;        /**         *  Whether the bootstrap loader is at the stage root or not.         *  It is only the stage root if it was the root         *  of the first SWF file that was loaded by Flash Player.         *  Otherwise, it could be a top-level application but not stage root         *  if it was loaded by some other non-Flex shell or is sandboxed.         */        private var isStageRoot:Boolean = true;        /**         *  Called when the bootstrap loader's SWF file has been loaded.         *  Starts loading the application SWF specified by the applicationURL property.         */        private function initHandler(event:Event):void {                loader = new Loader();                addChild(loader);                loader.load(new URLRequest(applicationURL));                loader.addEventListener("mx.managers.SystemManager.isBootstrapRoot",                         bootstrapRootHandler);                loader.addEventListener("mx.managers.SystemManager.isStageRoot",                         stageRootHandler);                stage.addEventListener(Event.RESIZE, resizeHandler);        }        private function bootstrapRootHandler(event:Event):void {                // Cancel event to indicate that the message was heard.                event.preventDefault();        }        private function stageRootHandler(event:Event):void {                // Cancel event to indicate that the message was heard.                if (!isStageRoot)                        event.preventDefault();        }        private function resizeHandler(event:Event):void {                loader.width = stage.width;                loader.height = stage.height;                Object(loader.content).setActualSize(stage.width, stage.height);        }}}

下面的图片说明了每个域通过定义了mx.messaging.*类的bootstrap loader来得到它的类定义:


原创粉丝点击