使用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. ...
複製代碼

 

發佈了94 篇原創文章 · 獲贊 13 · 訪問量 68萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章