OXM

O/X Mapper 是什么?  

Spring 3.0 的一个新特性是 O/X Mapper。O/X 映射器这个概念并不新鲜,O 代表 Object,X 代表 XML。它的目的是在 Java 对象(几乎总是一个 plain old Java object,或简写为 POJO)和 XML 文档之间来回转换。

例 如,您可能有一个带有几个属性的简单 bean,且您的业务需要将那个 Java 对象转换为一个 XML 文档。Spring 的 O/X Mapper 能够为您解决那个问题。如果反过来,您需要将一个 XML 文档转换为一个简单 Java bean,Spring 的 O/X Mapper 也能胜任。

有一点需要注意:Spring O/X Mapper 只是定义由流行的第三方框架实现的统一的界面。要利用 Spring 的 O/X 功能,您需要一个在 Java 对象和 XML 之间来回转换的实用程序。Castor 就是这样一个流行的第三方工具,本文将使用这个工具。其他这样的工具包括 XMLBeans、Java Architecture for XML Binding (JAXB)、JiBX 和 XStream。

编组和解组

进行 O/X 映射时,您经常会看到编组(marshalling)和解组(unmarshalling) 这两个术语。

编组 指将 Java bean 转换成 XML 文档的过程,这意味着 Java bean 的所有字段和字段值都将作为 XML 元素或属性填充到 XML 文件中。有时,编组也称为序列化(serializing)。

如您所料,解组 是与编组完全相反的过程,即将 XML 文档转换为 Java bean,这意味着 XML 文档的所有元素或属性都作为 Java 字段填充到 Java bean 中。有时,解组也称为反序列化(deserializing)。

使用 Spring 的 O/X Mapper 的好处

使 用 Spring 的 O/X Mapper 的一个最直接的好处是可以通过利用 Spring 框架的其他特性简化配置。Spring 的 bean 库支持将实例化的 O/X 编组器注入(即前面提到过的 “依赖项注入”)使用那些编组器的对象。重申一遍,这将加快应用程序开发和部署。

遵循坚实的面向对象的设计实践,Spring O/X 框架只定义两个接口:Marshaller 和 Unmarshaller,它们用于执行 O/X 功能,这是使用这个框架的另一个重大好处。这些接口的实现完全对独立开发人员开放,开发人员可以轻松切换它们而无需修改代码。例如,如果您一开始使用 Castor 进行 O/X 转换,但后来发现它缺乏您需要的某个功能,这时您可以切换到 XMLBeans 而无需任何代码更改。唯一需要做的就是更改 Spring 配置文件以使用新的 O/X 框架。

使用 Spring 的 O/X Mapper 的另一个好处是统一的异常层次结构。Spring 框架遵循使用它的数据访问模块建立的模式,方法是将原始异常对象包装到 Spring 自身专为 O/X Mapper 建立的运行时异常中。由于第三方提供商抛出的原始异常被包装到 Spring 运行时异常中,您能够查明出现异常的根本原因。您也不必费心修改代码以捕获异常,因为异常已包装到一个运行时异常中。以下几个运行时异常扩展了基础异常 XMLMappingException:GenericMarshallingFailureException、 ValidationFailureException、MarshallingFailureException 和 UnmarshallingFailureException。

入门栗子

配置清单:

applicationContext.xml Spring配置文件

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  4.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  5.     
  6.     <bean id="oxmDemo" class="com.mdf.springoxm.oxmDemo">  
  7.         <property name="marshaller" ref="castorMarshaller" />  
  8.         <property name="unmarshaller" ref="castorMarshaller" />  
  9.     </bean>  
  10.     <!-- 引入castor包:castor-1.3.2-core.jar,castor-1.3.2-xml.jar -->  
  11.     <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">  
  12.           <property name="mappingLocation" value="classpath:mapping.xml" />  
  13.     </bean>  
  14. </beans>  
编组和解组时,套用的mapping格式,在进行解组的时候,必须使用mapping才能成功(这里存在疑问,不知道是否因为自己写法问题,只能通过mapping进行格式编码才能进行解组,否则报出无法找到root element的错误)。
mapping.xml文件
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <mapping>  
  2.         <class name="com.mdf.springoxm.Customer">  
  3.     
  4.            <map-to xml="Customer"/>  
  5.     
  6.            <field name="flag" type="boolean">  
  7.               <bind-xml name="flag" node="element"/>  
  8.            </field>  
  9.                
  10.            <field name="name" type="string">  
  11.               <bind-xml name="name" node="element"/>  
  12.            </field>  
  13.     
  14.            <field name="sex" type="string">  
  15.               <bind-xml name="sex" node="element"/>  
  16.            </field>  
  17.         </class>  
  18. </mapping>  
自定义Bean文件

  Customer.java

  1. package com.mdf.springoxm;  
  2.   
  3. public class Customer {  
  4.     private String name;  
  5.     private String sex;  
  6.     private Boolean flag;  
  7.     public String getName() {  
  8.         return name;  
  9.     }  
  10.     public void setName(String name) {  
  11.         this.name = name;  
  12.     }  
  13.     public String getSex() {  
  14.         return sex;  
  15.     }  
  16.     public void setSex(String sex) {  
  17.         this.sex = sex;  
  18.     }  
  19.     public Boolean getFlag() {  
  20.         return flag;  
  21.     }  
  22.     public void setFlag(Boolean flag) {  
  23.         this.flag = flag;  
  24.     }  
  25. }  
xmlDemo.java文件
  1. package com.mdf.springoxm;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import javax.xml.transform.stream.StreamResult;  
  7. import javax.xml.transform.stream.StreamSource;  
  8. import org.springframework.oxm.Marshaller;  
  9. import org.springframework.oxm.Unmarshaller;  
  10.   
  11. public class oxmDemo{  
  12.   
  13.     private Marshaller marshaller;  
  14.     private Unmarshaller unmarshaller;  
  15.   
  16.     public Marshaller getMarshaller() {  
  17.         return marshaller;  
  18.     }  
  19.   
  20.     public void setMarshaller(Marshaller marshaller) {  
  21.         this.marshaller = marshaller;  
  22.     }  
  23.   
  24.     public Unmarshaller getUnmarshaller() {  
  25.         return unmarshaller;  
  26.     }  
  27.   
  28.     public void setUnmarshaller(Unmarshaller unmarshaller) {  
  29.         this.unmarshaller = unmarshaller;  
  30.     }  
  31.   
  32.     public void convertFromObjectToXML(Object object, String filepath)  
  33.         throws IOException {  
  34.   
  35.         FileOutputStream os = null;  
  36.         try {  
  37.             os = new FileOutputStream(filepath);  
  38.             getMarshaller().marshal(object, new StreamResult(os));  
  39.         } finally {  
  40.             if (os != null) {  
  41.                 os.close();  
  42.             }  
  43.         }  
  44.     }  
  45.   
  46.     public Object convertFromXMLToObject(String xmlfile) throws IOException {  
  47.   
  48.         FileInputStream is = null;  
  49.         try {  
  50.             is = new FileInputStream(xmlfile);  
  51.             return getUnmarshaller().unmarshal(new StreamSource(is));  
  52.         } finally {  
  53.             if (is != null) {  
  54.                 is.close();  
  55.             }  
  56.         }  
  57.     }  
  58.   
  59. }  

测试
  1. import org.springframework.context.ApplicationContext;  
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  3.   
  4. import com.mdf.springoxm.Customer;  
  5. import com.mdf.springoxm.oxmDemo;  
  6. import java.io.IOException;  
  7.   
  8.   
  9. public class Main {  
  10.     private static final String XML_FILE_NAME = "customer.xml";  
  11.       
  12.     public static void main(String[] args) throws IOException {  
  13.         ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");  
  14.         oxmDemo converter = (oxmDemo) appContext.getBean("oxmDemo");  
  15.           
  16.         Customer customer = new Customer();  
  17.         customer.setName("yiibai");  
  18.         customer.setFlag(true);  
  19.         customer.setSex("Haikou haidiandao");  
  20.           
  21.         System.out.println("Convert Object to XML!");  
  22.         //from object to XML file  
  23.         converter.convertFromObjectToXML(customer, XML_FILE_NAME);  
  24.         System.out.println("Done  ");  
  25.           
  26.         System.out.println("Convert XML back to Object!");  
  27.         //from XML to object  
  28.         Customer customer2 = (Customer)converter.convertFromXMLToObject(XML_FILE_NAME);  
  29.         System.out.println(customer2);  
  30.         System.out.println("Done");  
  31.           
  32.     }  
  33. }  

测试结果:
  1. 五月 11, 2016 2:27:52 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh  
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1221be2: startup date [Wed May 11 14:27:51 CST 2016]; root of context hierarchy  
  3. 五月 11, 2016 2:27:52 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
  4. 信息: Loading XML bean definitions from class path resource [applicationContext.xml]  
  5. 五月 11, 2016 2:27:52 下午 org.springframework.oxm.castor.CastorMarshaller afterPropertiesSet  
  6. 信息: Configured using [class path resource [mapping.xml]]  
  7. Convert Object to XML!  
  8. Done   
  9.   
  10. Convert XML back to Object!  
  11. com.mdf.springoxm.Customer@b419da  
  12. Done  



 
0
原文地址:https://www.cnblogs.com/sherrykid/p/5958519.html