問題
在使用IoC框架,比如Spring ActionScript的時候,因爲代碼中沒有對類的引用,Flex並不會自動將所需的類編譯到主SWF中去,這就會產生一個問題,即運行時找不到所需的類,這也是困惑Spring ActionScript新手的一個問題。那麼如何解決呢,以下內容譯自Spring ActionScript的官方文檔。
原文地址:
http://www.springactionscript.org/docs/reference/html/Class-inclusion.html
解決方案(根據你的需要選擇一種方式)
1.在你的代碼中任何位置加入對類的引用:
- {
- Myclass1, Myclass2
- }
2.創建變量或數組加入對類的引用:
- private var _includeClass:Array = [Myclass1,Myclass2];
-
private var _includeClass:Array = [Myclass1,Myclass2];
3.使用Frame metadata:
- package com.myclasses
- {
- [Frame(extraClass="com.myclasses.Myclass1")]
- [Frame(extraClass="com.myclasses.Myclass2")]
- public class MyMainClass
- {
- }
- }
4.使用resource bundle:
在你的項目中創建一個名爲 classreferences.properties 的文件並添加你的類進入,比如:
- Class1 = ClassReference("com.myclasses.Myclass1")
- Class2 = ClassReference("com.myclasses.Myclass2")
然後在你的代碼中添加對資源的引用:
- [ResourceBundle("classreferences")]
- private var _classReferences:ResourceBundle;
-
[ResourceBundle("classreferences")]
-
private var _classReferences:ResourceBundle;
5.使用ANT工具作爲一個預加載器生成編譯配置文件
第一步,添加一個編譯參數到你的項目,如下圖所示(注意Additional compiler arguments參數):
第二步,創建一個XSLT ANT任務到你的項目根目錄(注意不是src),命名爲'springasprebuild.xml',並添加下面的代碼:
- <!--
- ======================================================================
- Nov 17, 2008 4:33:15 PM
- project SpringAS Pre build
- description Extracts all the class paths from a Spring Actionscript
- configuration file and dumps them into
- a config file that can be added with a compiler
- switch (i.e. -load-config+=custom.config)
- Roland Zwaga
- ====================================================================== -
- ->
- <project name="project" default="default">
- <description>
- Extracts all the class paths from a Spring Actionscript configuration file and
- dumps them in a config file that can be added with a compiler switch
- (i.e. -load-config+=custom.config)
- </description>
- <!--
- =================================
- target: default
- =================================
- -->
- <target name="default" description="description">
- <xslt in="src/application-context.xml" out="src/springas.config"
- style="xslt/springasconfig.xsl" reloadstylesheet="false"></xslt>
- </target>
- </project>
第三步,在你的項目根目錄創建一個"xslt"的目錄,然後在這個目錄中創建一個新的文件叫做"springasconfig.xsl",並添加下面的代碼:
- <?xml version="1.0" encoding="UTF-8"?>
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:springas="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 "
- exclude-result-prefixes="xsi springas">
- <xsl:output method="xml" indent="yes"/>
- <xsl:key name="restriction" match="//springas:object" use="@class" />
- <xsl:key name="proprestriction" match="//springas:property"
- use="@value" />
- <xsl:key name="valrestriction" match="//springas:value" use="." />
- <xsl:template match="/">
- <flex-config>
- <includes append="true">
- <xsl:for-each
- select="//springas:object[count(.|key('restriction',@class)[1]) = 1]">
- <xsl:sort select="@class"/>
- <xsl:if test="@class!=''">
- <symbol>
- <xsl:value-of select="@class"/>
- </symbol>
- </xsl:if>
- </xsl:for-each>
- <xsl:for-each
- select="//springas:property[count(.|key('proprestriction',@value)[1]) = 1]">
- <xsl:sort select="@value"/>
- <xsl:if test="@type='Class'">
- <symbol>
- <xsl:value-of select="@value"/>
- </symbol>
- </xsl:if>
- </xsl:for-each>
- <xsl:for-each
- select="//springas:value[count(.|key('valrestriction',.)[1]) = 1]">
- <xsl:sort select="."/>
- <xsl:if test="@type='Class'">
- <symbol>
- <xsl:value-of select="."/>
- </symbol>
- </xsl:if>
- </xsl:for-each>
- </includes>
- </flex-config>
- </xsl:template>
- </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"的文件,文件內容類似於這樣:
- <?xml version="1.0" encoding="UTF-8"?>
- <flex-config>
- <includes append="true">
- <symbol>mx.messaging.channels.AMFChannel</symbol>
- <symbol>mx.messaging.ChannelSet</symbol>
- <symbol>mx.rpc.remoting.mxml.RemoteObject</symbol>
- </includes>
- </flex-config>
好了,完工
6.使用Maven。你需要先點擊這裏下載mojo文件,然後打開運行"mvn install"將它加入到你的本地倉庫。
然後像下面的方式一樣使用:
- ...
- <!-- set up properties to make it easy for both plugins (the prebuild and the maven-mojo) to use the same file locations -->
- <properties>
- <generatedCompilerConfigName>spring-as-includes.config</generatedCompilerConfigName>
- <generatedCompilerConfigDir>${project.build.sourceDirectory}</generatedCompilerConfigDir>
- </properties>
- <build>
- ...
- <plugins>
- <plugin>
- <groupId>org.springextensions.actionscript</groupId>
- <artifactId>prebuild-mojo</artifactId>
- <version>0.1-SNAPSHOT</version>
- <executions>
- <execution>
- <phase>generate-resources</phase>
- <goals>
- <goal>generate-compiler-config</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <springActionscriptContextFiles>
- <param>${basedir}/src/main/flex/YOUR-CONTEXT-FILE.xml</param>
- </springActionscriptContextFiles>
- <outputDirectory>${generatedCompilerConfigDir}</outputDirectory>
- <generatedConfigFileName>${generatedCompilerConfigName}</generatedConfigFileName>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.sonatype.flexmojos</groupId>
- <artifactId>flexmojos-maven-plugin</artifactId>
- <version>3.6-SNAPSHOT</version>
- <configuration>
- ...
- <configFiles>
- <file>${generatedCompilerConfigDir}/${generatedCompilerConfigName}</file>
- </configFiles>
- ...
- </configuration>
- </plugin>
- </plugins>
- </build>
- ...