问题
在使用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>
- ...