17.3 Marshaller 与 Unmarshaller 的使用

Spring 的 OXM 可被用于十分广泛的场景。在以下的例子中,我们将使用这一功能将一个由 Spring 管理的应用程序的配置编组为一个 XML 文件。我们用了一个简单的 JavaBean 来表示这些配置:

  1. public class Settings {
  2. private boolean fooEnabled;
  3. public boolean isFooEnabled() {
  4. return fooEnabled;
  5. }
  6. public void setFooEnabled(boolean fooEnabled) {
  7. this.fooEnabled = fooEnabled;
  8. }
  9. }

应用程序的主类使用这个 bean 来存放应用的配置信息。除了主要方法外,主类还包含下面两个方法:saveSettings() 将配置 bean 保存成一个名为 settings.xml 的文件, loadSettings() 则将配置信息从 XML 文件中读取出来。另有一个 main() 方法负责构建 Spring 应用上下文,并调用前述两个方法。

  1. import java.io.FileInputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import javax.xml.transform.stream.StreamResult;
  5. import javax.xml.transform.stream.StreamSource;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. import org.springframework.oxm.Marshaller;
  9. import org.springframework.oxm.Unmarshaller;
  10. public class Application {
  11. private static final String FILE_NAME = "settings.xml";
  12. private Settings settings = new Settings();
  13. private Marshaller marshaller;
  14. private Unmarshaller unmarshaller;
  15. public void setMarshaller(Marshaller marshaller) {
  16. this.marshaller = marshaller;
  17. }
  18. public void setUnmarshaller(Unmarshaller unmarshaller) {
  19. this.unmarshaller = unmarshaller;
  20. }
  21. public void saveSettings() throws IOException {
  22. FileOutputStream os = null;
  23. try {
  24. os = new FileOutputStream(FILE_NAME);
  25. this.marshaller.marshal(settings, new StreamResult(os));
  26. } finally {
  27. if (os != null) {
  28. os.close();
  29. }
  30. }
  31. }
  32. public void loadSettings() throws IOException {
  33. FileInputStream is = null;
  34. try {
  35. is = new FileInputStream(FILE_NAME);
  36. this.settings = (Settings) this.unmarshaller.unmarshal(new StreamSource(is));
  37. } finally {
  38. if (is != null) {
  39. is.close();
  40. }
  41. }
  42. }
  43. public static void main(String[] args) throws IOException {
  44. ApplicationContext appContext =
  45. new ClassPathXmlApplicationContext("applicationContext.xml");
  46. Application application = (Application) appContext.getBean("application");
  47. application.saveSettings();
  48. application.loadSettings();
  49. }
  50. }

需要将 marshaller 和 unmarshaller 这两个属性赋值才能使 Application 正确运行。我们可以使用以下 applicationContext.xml 的内容来实现这一目的:

  1. <beans>
  2. <bean id="application" class="Application">
  3. <property name="marshaller" ref="castorMarshaller" />
  4. <property name="unmarshaller" ref="castorMarshaller" />
  5. </bean>
  6. <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/>
  7. </beans>

该应用中使用了 Castor 这一编组器实例,但我们可以使用在本章稍后描述的任何一个编组器实例来替换 Castor。Castor 默认并不需要任何进一步的配置,所以 bean 定义十分简洁。另外由于 CastorMarshaller 同时实现了 Marshaller 与 Unmarshaller 接口,所以我们可以同时把 castorMarshaller bean 赋值给应用的 marshaller 与 unmarshaller 属性。

此范例应用将会产生如下 settings.xml 文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <settings foo-enabled="false"/>