Flex中的配置文件存储 Configuration Storage in Flex/Actionscript

Gloss PNGTurbo Navigator Those who have Java experience should be very familiar with Java's configuration storage mechanism. You throw your configuration into an XML file and load it from the JAR file. However, in Flex, there is no way to read resources other than sprites or graphics from SWC/SWF files.

How do you store your configuration data in Flex? Basically, there are four ways:

1. Store configuration in static constants

e.g.,

public class MyApp {
  public static const config:XML = <config><property name="id" value="1"/></config>;
}

2. Store configuration in a resource bundle

e.g.:

// MyApp_locale.properties:

myapp_cofig: <config><property name="id" value="1"/></config>;

Technically, it's the same with method 1 since the compiler will ultimately convert your resource bundle to a class.

3. Store configuration in metadata

[MyConfig(name="id", value="1")]
public class MyApp { ... }

Then at the runtime, you can use flash.utils.describeType(new MyApp()) to obtain the metadata in XML format.

Note: You need to append -keep-as3-metadata+=MyConfig to your compiler options. Otherwise, the compiler will ignore your metadata.

4. Probably the best way - <mx:XML>

<mx:XML id="xmlModulesConfig" source="modules.xml"/>