`
hgfghwq15
  • 浏览: 49756 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

Parsley初次应用

阅读更多

  Parsley是一个比较著名的Flex IoC开源项目,也被adobe所推荐。但在最开始的使用上并不见得那么容易,尤其是在使用XML作为配置文件的时候。
  首先考虑用MXML做配置文件,用这种方式是比较容易上手使用的。考虑我们有一个接口LoginService与其实现LoginServiceImpl,以及一个LoginAction类。功能很简单,如下所示。其中LoginAction中引用了LoginService接口,我们希望注入为LoginServiceImpl。 package com.dream.actions { import com.dream.services.LoginService; import mx.controls.Alert; public class LoginAction { [Inject] public var service : LoginService; public function LoginAction():void { trace("loginAction constructor"); } [MessageHandler] public function doAction(event:LoginEvent):void { Alert.show("doAction"); service.login(); } } } package com.dream.actions { import flash.events.Event; public class LoginEvent extends Event { public static const LOGIN_EVENT:String = "login"; public function LoginEvent(type:String = LOGIN_EVENT, bubbles:Boolean=false, cancelable:Boolean=false) { super(type, bubbles, cancelable); } } } package com.dream.services { public interface LoginService { function login():void; } } package com.dream.services { import mx.controls.Alert; public class LoginServiceImpl implements LoginService { public function LoginServiceImpl() { trace("LoginServiceImp constructor"); } public function login():void { Alert.show("login"); trace("login"); } } } 在Application页面中,我们只有一个Button,点击该Button发出一个事件LoginEvent,该事件我们注册为ManagedEvents,这样就可以让LoginAction中的doAction接受到。这运用的是Parsley的消息机制。下面是Application对应的代码  flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:spicefactory="http://www.spicefactory.org/pa rsley" minWidth="955" minHeight="600" addedToStage="this.dispatchEvent(new Event('configureView', true))" >   [ManagedEvents("login")]                接下来我们看一下如何利用xml进行配置。利用xml进行配置好处在于可以方便修改配置而不用重新编译,但是这样也有很多值得注意的细微之处,刚用的时候很容易犯错误。
  LoginService,LoginServiceImpl和LoginAction都跟原来的一样。不同的是我们在src根目录下增加了config.xml,其内容如下      相应的application mxml也做了一定的修改。代码如下。  flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:spicefactory="http://www.spicefactory.org/pa rsley" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHan dler(event)" addedToStage="this.dispatchEvent(new Event('configureView', true))" >   [ManagedEvents("login")]    -->   加载config.xml时发生找不到类的情况 LoginServiceImpl; LoginAction; ]]-->     这里重要的几点要提出,
  (1)利用xml配置,如果在代码中声明了接口属性(如LoginAction中的LoginService service属性),但是在xml中配置了该接口的实现,如本例config.xml中的LoginServiceImpl。由于在代码中没有直接引用LoginServiceImpl,这样编译的时候并不会编译LoginServiceImpl,以至于在运行时,你会发现在IoC初始化的过程中抛出类似于如下的错误
  cause(1): Error: Unknown class: com.dream.services.LoginServiceImpl
  ERROR: Error processing [object XmlConfigurationProcessor]: One or more errors in XmlObjectDefinitionBuilder:
  Error processing file [XmlFile config.xml]:
  Error processing element http://www.spicefactory.org/parsley::objects
  所以在Application mxml页面中你会发现有这么两行看似没有意义的代码。这两行的加入就会将相应的类编译进来。
  LoginServiceImpl;
  LoginAction;
  这个说明在Parsely的官方文档中可以找到,但是一定要仔细看才能知道,太可惜没有相应的例子了。参考http://www.spicefactory.org/parsley/docs/2.0/manua l/config.php#xml  Compiling classes configured in XML  One thing you need to be aware of is that in contrast to MXML configuration the classes you use in the configuration file will not be compiled into your SWC or SWF if you don't use them explicitly in your code. This might happen quite often since it is good practice to program against interfaces and only declare the concrete implementation in the container configuration. There are basically three choices to solve this:  Add a reference to these classes explicity in your code even though it is not needed. Most developers consider this an ugly hack. 
  If you want to use these classes as a library, compile them into an SWC (with compc you can include whole source folders into the SWC) and then include the whole SWC into your SWF with the -include-libraries option of the mxmlc compiler. 
  You can alternatively include individual classes with the -includes option of the mxmlc compiler. 
  (2)在Application节点中我们有这么一行代码addedToStage="this.dispatchEvent(new Event('configureView', true))"。这个非常重要,因为没有这一行而只有creationComplete对应的build xml的话,虽然config.xml中定义的类会实例化了,但是并没有加到IoC容器中,即使你在mxml中显示的增加[Inject] public var loginAction,也会发现其实loginAction是null,而sendEvent属性也是null。只有增加了这一行代码,才会将LoginAction, LoginServiceImpl实例加到IoC容器中。总之,这一行代码就是告诉IoC容器要将实例注入到这个页面。具体可以参看http://www.spicefactory.org/parsley/docs/2.0/manua l/view.php#intro, 在这篇文章中也可以找到相关的说明http://www.kalengibbons.com/blog/index.php/2009/11 /learning-parsley-part-1-dependency-injection/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics