使用Spring ActionScript的时候编译所需类的6种方式

问题

在使用IoC框架,比如Spring ActionScript的时候,因为代码中没有对类的引用,Flex并不会自动将所需的类编译到主SWF中去,这就会产生一个问题,即运行时找不到所需的类,这也是困惑Spring ActionScript新手的一个问题。那么如何解决呢,以下内容译自Spring ActionScript的官方文档。

原文地址:

http://www.springactionscript.org/docs/reference/html/Class-inclusion.html

解决方案(根据你的需要选择一种方式)

1.在你的代码中任何位置加入对类的引用:

  1. {
  2. Myclass1, Myclass2
  3. }
复制代码

2.创建变量或数组加入对类的引用:

  1. private var _includeClass:Array = [Myclass1,Myclass2];
普通浏览 复制代码 保存代码 打印代码
  1. private var _includeClass:Array =  [Myclass1,Myclass2 ] ;

3.使用Frame metadata:

  1. package com.myclasses
  2. {
  3. [Frame(extraClass="com.myclasses.Myclass1")]
  4. [Frame(extraClass="com.myclasses.Myclass2")]
  5. public class MyMainClass
  6. {
  7. }
  8. }
复制代码

4.使用resource bundle:

在你的项目中创建一个名为 classreferences.properties 的文件并添加你的类进入,比如:

  1. Class1   = ClassReference("com.myclasses.Myclass1")
  2. Class2   = ClassReference("com.myclasses.Myclass2")
复制代码

然后在你的代码中添加对资源的引用:

  1. [ResourceBundle("classreferences")]
  2. private var _classReferences:ResourceBundle;
普通浏览 复制代码 保存代码 打印代码
  1. [ResourceBundle ( "classreferences" ) ]
  2. private var _classReferences:ResourceBundle ;

5.使用ANT工具作为一个预加载器生成编译配置文件

第一步,添加一个编译参数到你的项目,如下图所示(注意Additional compiler arguments参数):

第二步,创建一个XSLT ANT任务到你的项目根目录(注意不是src),命名为'springasprebuild.xml',并添加下面的代码:

  1. <!--
  2. ======================================================================
  3. Nov 17, 2008 4:33:15 PM
  4. project SpringAS Pre build
  5. description Extracts all the class paths from a Spring Actionscript
  6. configuration file and dumps them into
  7. a config file that can be added with a compiler
  8. switch (i.e. -load-config+=custom.config)
  9. Roland Zwaga
  10. ====================================================================== -
  11. ->
  12. <project name="project" default="default">
  13. <description>
  14. Extracts all the class paths from a Spring Actionscript configuration file and
  15. dumps them in a config file that can be added with a compiler switch
  16. (i.e. -load-config+=custom.config)
  17. </description>
  18. <!--
  19. =================================
  20. target: default
  21. =================================
  22. -->
  23. <target name="default" description="description">
  24. <xslt in="src/application-context.xml" out="src/springas.config"
  25. style="xslt/springasconfig.xsl" reloadstylesheet="false"></xslt>
  26. </target>
  27. </project>
复制代码

第三步,在你的项目根目录创建一个"xslt"的目录,然后在这个目录中创建一个新的文件叫做"springasconfig.xsl",并添加下面的代码:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0"
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4. xmlns:springas="http://www.springactionscript.org/schema/objects"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springactionscript.org/schema/objects
  7. http://www.springactionscript.org/schema/objects/spring-actionscript-objects-1.0.xsd "
  8. exclude-result-prefixes="xsi springas">
  9. <xsl:output method="xml" indent="yes"/>
  10. <xsl:key name="restriction" match="//springas:object" use="@class" />
  11. <xsl:key name="proprestriction" match="//springas:property"
  12. use="@value" />
  13. <xsl:key name="valrestriction" match="//springas:value" use="." />
  14. <xsl:template match="/">
  15. <flex-config>
  16. <includes append="true">
  17. <xsl:for-each
  18. select="//springas:object[count(.|key('restriction',@class)[1]) = 1]">
  19. <xsl:sort select="@class"/>
  20. <xsl:if test="@class!=''">
  21. <symbol>
  22. <xsl:value-of select="@class"/>
  23. </symbol>
  24. </xsl:if>
  25. </xsl:for-each>
  26. <xsl:for-each
  27. select="//springas:property[count(.|key('proprestriction',@value)[1]) = 1]">
  28. <xsl:sort select="@value"/>
  29. <xsl:if test="@type='Class'">
  30. <symbol>
  31. <xsl:value-of select="@value"/>
  32. </symbol>
  33. </xsl:if>
  34. </xsl:for-each>
  35. <xsl:for-each
  36. select="//springas:value[count(.|key('valrestriction',.)[1]) = 1]">
  37. <xsl:sort select="."/>
  38. <xsl:if test="@type='Class'">
  39. <symbol>
  40. <xsl:value-of select="."/>
  41. </symbol>
  42. </xsl:if>
  43. </xsl:for-each>
  44. </includes>
  45. </flex-config>
  46. </xsl:template>
  47. </xsl:stylesheet>
复制代码

添加ANT任务到你的项目,右键点击你的项目,选择"Properties",然后再选择"Builders"

点击"New"并选择"ANT Builder"并点击"OK",出现对话框。给Builder创建一个名称,比如"Spring AS Builder",然后选择点击"Browse workspace",在接下来的对话框中选择你项目根目录中的build文件,比如"springasprebuild.xml"

点击“OK”返回Builder列表,你会发现顺序是错的,点击"UP"将SpringAS_Builder移到第一位

点击"OK"完成,项目就配置好了。测试以下,编译你的项目,你的源码目录下面创建了一个名为"springas.config"的文件,文件内容类似于这样:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <flex-config>
  3. <includes append="true">
  4. <symbol>mx.messaging.channels.AMFChannel</symbol>
  5. <symbol>mx.messaging.ChannelSet</symbol>
  6. <symbol>mx.rpc.remoting.mxml.RemoteObject</symbol>
  7. </includes>
  8. </flex-config>
复制代码

好了,完工

6.使用Maven。你需要先点击这里下载mojo文件,然后打开运行"mvn install"将它加入到你的本地仓库。

然后像下面的方式一样使用:

  1. ...
  2. <!-- set up properties to make it easy for both plugins (the prebuild and the maven-mojo) to use the same file locations -->
  3. <properties>
  4. <generatedCompilerConfigName>spring-as-includes.config</generatedCompilerConfigName>
  5. <generatedCompilerConfigDir>${project.build.sourceDirectory}</generatedCompilerConfigDir>
  6. </properties>
  7. <build>
  8. ...
  9. <plugins>
  10. <plugin>
  11. <groupId>org.springextensions.actionscript</groupId>
  12. <artifactId>prebuild-mojo</artifactId>
  13. <version>0.1-SNAPSHOT</version>
  14. <executions>
  15. <execution>
  16. <phase>generate-resources</phase>
  17. <goals>
  18. <goal>generate-compiler-config</goal>
  19. </goals>
  20. </execution>
  21. </executions>
  22. <configuration>
  23. <springActionscriptContextFiles>
  24. <param>${basedir}/src/main/flex/YOUR-CONTEXT-FILE.xml</param>
  25. </springActionscriptContextFiles>                   
  26. <outputDirectory>${generatedCompilerConfigDir}</outputDirectory>
  27. <generatedConfigFileName>${generatedCompilerConfigName}</generatedConfigFileName>
  28. </configuration>
  29. </plugin>
  30. <plugin>
  31. <groupId>org.sonatype.flexmojos</groupId>
  32. <artifactId>flexmojos-maven-plugin</artifactId>
  33. <version>3.6-SNAPSHOT</version>
  34. <configuration>
  35. ...
  36. <configFiles>
  37. <file>${generatedCompilerConfigDir}/${generatedCompilerConfigName}</file>
  38. </configFiles>
  39. ...
  40. </configuration>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. ...
复制代码

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值