美文网首页
Spring OXM

Spring OXM

作者: 码而优则仕 | 来源:发表于2020-06-14 22:44 被阅读0次

    Spring OXM

    1. Spring OXM 概述

      Spring OXM 是Spring3.0的一个新特性,为主流O/X Mapping组件提供了统一层抽象和封装,而在Spring4.0中OXM没有增加更多新特性。O/X映射器这个概念并不新鲜,O 代表 Object,X代表XML,目的是在Java对象和XML之间进行转换操作。Spring OXM 不仅仅屏蔽了各O/XMapping组件实现的差异性,而且还提供了统一,高效的编程模型。Spring OXM的结构框架如下所示:

    OXM架构图.png
    Marshaller和Unmarshaller是Spring OXM的两个核心接口。实现Marshaller接口可以实现从Java对象到XML的映射转换,实现Unmarshaller接口可实现从XML到Java对象的映射转换。
    
    Marshaller接口定义:
    
    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n522" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, &quot;Andale Mono&quot;, &quot;DejaVu Sans Mono&quot;, monospace; margin-top: 0px; margin-bottom: 20px; background-color: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; background-position: initial initial; background-repeat: initial initial;">public interface Marshaller {
     //判断支持的编组类类型
     boolean supports(Class<?> class);
    ​
     //对传入对象进行编组操作
     void marshal(Object graph, Result result) throws IOException, XmlMappingException;
    }</pre>
    
    Marshaller接口中提供了两个方法,其中supports()方法用于判断支持的编组类类型;marshal 方法用于对对象进行编组操作,其中graph参数为目标转换对象,result参数为JDK提供的XML转换接口,实现此接口的对象包含构建转换结果树所需的信息。**Spring OXM支持的Result转换实现类有 DOMResult,SAXResult,StreamResult,StaxResult**,其中 DOMResult,SAXResult,StreamResult为JDK提供的实现类,StaxResult为Spring OXM提供的扩展类。
    
    Unmarshaller接口定义
    
    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n532" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, &quot;Andale Mono&quot;, &quot;DejaVu Sans Mono&quot;, monospace; margin-top: 0px; margin-bottom: 20px; background-color: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; background-position: initial initial; background-repeat: initial initial;">public interface Unmarshaller {
    ​
     //
     boolean supports(Class<?> clazz);
    //
     Object unmarshal(Source source) throws IOException, XmlMappingException;
    ​
    }</pre>
    
    Unmarshaller接口提供了两个方法,其中supports方法用于判断支持的反编组类类型,unmarshal方法用于将输入源对象进行反编组操作,其中source参数为目标输入源对象,Source参数为JDK提供的输入源接口,实现此接口的对象包含充当源输入(XML源或转换指令)所需的信息。Spring OXM 支持的Source输入源实现类有 DOMSource,SAXSource,StreamSource,StaxSource,其中 DOMSource,SAXSource,StreamSource为JDK提供实现类,StaxSource为Spring OXM提供的扩展类。
    
    Spring OXM统一封装底层的O/XMapping组件异常,将各O/XMapping组件原始异常对象包装到Spring自身专为OXM建立的运行时异常 XmlMappingException中,并通过MarshallerFailureException和UnmarshallerFailureException处理编组和反编组操作之间的区别。
    
    1. 整合OXM实现者

      Spring OXM默认提供目前几个主流O/X Mapping组件的实现,包括XStream,XMLBeans,Castor,JiBX,JAXB。这些O/XMapping组件统一实现Spring OXM 的两个核心接口 Marshaller和Unmarshaller。

      要使用Spring OXM 的 O/X功能,首先需要一个在Java对象和XML之间来回转换的组件,并将上文提到的XStream,JAXB等的相应的类库添加到工程中。各O/XMapping组件对应的实现类如下:

      O/X Mapping组件 Spring OXM实现类
      XStream org.springframework.oxm.xstream.XStreamMarshaller
      Castor org.springframework.oxm.castor.CastorMarshaller
      JiBX org.springframework.oxm.jibx.JibxMarshaller
      JAXB org.springframework.oxm.jaxb.Jaxb2Marshaller
    2. 如何在Spring中进行配置

      到目前为止,我们已经介绍了各 O/X Mapping组件的使用方法以及Spring OXM整体框架,接下来介绍如何在Spring中整合这些 O/XMapping组件。

      spring.schemas 文件

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml-dtd" cid="n588" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-color: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; background-position: initial initial; background-repeat: initial initial;">http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd=org/springframework/oxm/config/spring-oxm-3.0.xsd
      http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd=org/springframework/oxm/config/spring-oxm-3.1.xsd
      http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd=org/springframework/oxm/config/spring-oxm-3.2.xsd
      http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd=org/springframework/oxm/config/spring-oxm-4.0.xsd
      http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd=org/springframework/oxm/config/spring-oxm-4.1.xsd
      http://www.springframework.org/schema/oxm/spring-oxm-4.2.xsd=org/springframework/oxm/config/spring-oxm-4.2.xsd
      http://www.springframework.org/schema/oxm/spring-oxm.xsd=org/springframework/oxm/config/spring-oxm-4.2.xsd</pre>

      spring-oxm-4.2.xsd文件:

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n594" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-color: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; background-position: initial initial; background-repeat: initial initial;"><?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <xsd:schema xmlns="http://www.springframework.org/schema/oxm" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:tool="http://www.springframework.org/schema/tool"
      targetNamespace="http://www.springframework.org/schema/oxm"
      elementFormDefault="qualified"
      attributeFormDefault="unqualified">

      <xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"/>
      <xsd:import namespace="http://www.springframework.org/schema/tool" schemaLocation="http://www.springframework.org/schema/tool/spring-tool-4.2.xsd"/>

      <xsd:annotation>
      <xsd:documentation>
      Defines the elements used in Spring's Object/XML Mapping integration.
      </xsd:documentation>
      </xsd:annotation>

      <xsd:element name="jaxb2-marshaller">
      <xsd:complexType>
      <xsd:annotation>
      <xsd:documentation source="java:org.springframework.oxm.jaxb.Jaxb2Marshaller">
      Defines a JAXB2 Marshaller.
      </xsd:documentation>
      <xsd:appinfo>
      <tool:annotation>
      <tool:exports type="org.springframework.oxm.jaxb.Jaxb2Marshaller"/>
      </tool:annotation>
      </xsd:appinfo>
      </xsd:annotation>
      <xsd:complexContent>
      <xsd:extension base="beans:identifiedType">
      <xsd:sequence>
      <xsd:element name="class-to-be-bound" minOccurs="0" maxOccurs="unbounded">
      <xsd:complexType>
      <xsd:attribute name="name" type="classType" use="required"/>
      </xsd:complexType>
      </xsd:element>
      </xsd:sequence>
      <xsd:attribute name="context-path" type="xsd:string">
      <xsd:annotation>
      <xsd:documentation>The JAXB context path.</xsd:documentation>
      </xsd:annotation>
      </xsd:attribute>
      </xsd:extension>
      </xsd:complexContent>
      </xsd:complexType>
      </xsd:element>

      <xsd:element name="jibx-marshaller">
      <xsd:complexType>
      <xsd:annotation>
      <xsd:documentation source="java:org.springframework.oxm.jibx.JibxMarshaller">
      Defines a JiBX Marshaller.
      </xsd:documentation>
      <xsd:appinfo>
      <tool:annotation>
      <tool:exports type="org.springframework.oxm.jibx.JibxMarshaller"/>
      </tool:annotation>
      </xsd:appinfo>
      </xsd:annotation>
      <xsd:complexContent>
      <xsd:extension base="beans:identifiedType">
      <xsd:attribute name="target-class" type="classType">
      <xsd:annotation>
      <xsd:documentation>The target class to be bound with JiBX.</xsd:documentation>
      </xsd:annotation>
      </xsd:attribute>
      <xsd:attribute name="target-package" type="xsd:string">
      <xsd:annotation>
      <xsd:documentation>The target package for the JiBX binding.</xsd:documentation>
      </xsd:annotation>
      </xsd:attribute>
      <xsd:attribute name="binding-name" type="xsd:string">
      <xsd:annotation>
      <xsd:documentation>The binding name used by this marshaller.</xsd:documentation>
      </xsd:annotation>
      </xsd:attribute>
      </xsd:extension>
      </xsd:complexContent>
      </xsd:complexType>
      </xsd:element>

      <xsd:element name="castor-marshaller">
      <xsd:complexType>
      <xsd:annotation>
      <xsd:documentation
      source="java:org.springframework.oxm.castor.CastorMarshaller">
      Defines a Castor Marshaller.
      </xsd:documentation>
      <xsd:appinfo>
      <tool:annotation>
      <tool:exports type="org.springframework.oxm.castor.CastorMarshaller" />
      </tool:annotation>
      </xsd:appinfo>
      </xsd:annotation>
      <xsd:complexContent>
      <xsd:extension base="beans:identifiedType">
      <xsd:attribute name="encoding" type="xsd:string">
      <xsd:annotation>
      <xsd:documentation>The encoding to use for stream reading.</xsd:documentation>
      </xsd:annotation>
      </xsd:attribute>
      <xsd:attribute name="target-class" type="classType">
      <xsd:annotation>
      <xsd:documentation>The target class to be bound with the Castor marshaller.</xsd:documentation>
      </xsd:annotation>
      </xsd:attribute>
      <xsd:attribute name="target-package" type="xsd:string">
      <xsd:annotation>
      <xsd:documentation>The target package that contains Castor descriptor classes.</xsd:documentation>
      </xsd:annotation>
      </xsd:attribute>
      <xsd:attribute name="mapping-location" type="xsd:string">
      <xsd:annotation>
      <xsd:documentation>The path to the Castor mapping file.</xsd:documentation>
      </xsd:annotation>
      </xsd:attribute>
      </xsd:extension>
      </xsd:complexContent>
      </xsd:complexType>
      </xsd:element>

      <xsd:simpleType name="classType">
      <xsd:annotation>
      <xsd:documentation source="java:java.lang.Class">A class supported by a marshaller.</xsd:documentation>
      <xsd:appinfo>
      <tool:annotation kind="direct">
      <tool:expected-type type="java.lang.Class"/>
      <tool:assignable-to restriction="class-only"/>
      </tool:annotation>
      </xsd:appinfo>
      </xsd:annotation>
      <xsd:union memberTypes="xsd:string"/>
      </xsd:simpleType>

      </xsd:schema></pre>

    相关文章

      网友评论

          本文标题:Spring OXM

          本文链接:https://www.haomeiwen.com/subject/qdhqxktx.html