Spring 5.2.2 數據訪問(34)

      接着Spring 5.2.2 數據訪問(33)講O-X映射。javascript

XML配置命名空間java

    經過使用OXM命名空間中的標記,能夠更簡潔地配置Marshaller 。要使這些標記可用,必須首先在XML配置文件的前導部分中引用適當的模式。下面的示例演示瞭如何執行此操做:web

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    //引用oxm模式 xmlns:oxm="http://www.springframework.org/schema/oxm" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd //指定oxm模式位置 http://www.springframework.org/schema/oxm https://www.springframework.org/schema/oxm/spring-oxm.xsd"

schema確保如下元素可用:spring

  • jaxb2-marshallerapi

  • jibx-marshaller數組

每一個標記都在其各自的Marshaller 部分中解釋。不過,做爲一個示例,JAXB2Marshaller 的配置可能相似於如下內容:安全

<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>


JAXB

     JAXB綁定編譯器將W3C XML模式轉換爲一個或多個Java類、一個jaxb.properties文件,可能還有一些資源文件。JAXB還提供了一種從帶註解的Java類生成模式的方法。微信

     Spring支持JAXB 2.0  api做爲XML編組策略,遵循Marshaller Unmarshaller 中描述的Marshaller Unmarshaller 接口。相應的集成類位於org.springframework.oxm.jaxb包中。app


使用Jaxb2Marshaller框架

     Jaxb2Marshaller 實現了Spring的Marshaller Unmarshaller 接口。它須要一個上下文路徑來操做。能夠經過設置contextPath 屬性來設置上下文路徑。上下文路徑是一個冒號分隔的Java包名稱列表,其中包含Schema 派生類。它還提供了一個classesToBeBound 屬性,容許你設置Marshaller 支持的類數組。Schema 驗證是經過向bean指定一個或多個Schema 資源來執行的,以下例所示:

<beans> <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="classesToBeBound"> <list> <value>org.springframework.oxm.jaxb.Flight</value> <value>org.springframework.oxm.jaxb.Flights</value> </list> </property> <property name="schema" value="classpath:org/springframework/oxm/schema.xsd"/> </bean>
...
</beans>

XML配置命名空間

   jaxb2-marshaller元素配置org.springframework.oxm.jaxb.Jaxb2Marshaller,以下例所示:

<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>

或者,能夠經過使用要綁定的類子元素提供要綁定到Marshaller的類列表:

<oxm:jaxb2-marshaller id="marshaller"> <oxm:class-to-be-bound name="org.springframework.ws.samples.airline.schema.Airport"/> <oxm:class-to-be-bound name="org.springframework.ws.samples.airline.schema.Flight"/> ...</oxm:jaxb2-marshaller>

下表描述了可用的屬性:


JiBX

    JiBX框架提供了一個相似於Hibernate爲ORM提供的解決方案:綁定定義定義了Java對象如何轉換成XML或從XML轉換成XML的規則。在準備綁定和編譯類以後,JiBX綁定編譯器加強了類文件,並添加了代碼來處理類實例從XML到XML的轉換。Spring集成類位於org.springframework.oxm.jibx包中。


使用JibxMarshaller

     JibxMarshaller 類實現Marshaller Unmarshaller 接口。要進行操做,它須要Marshaller 的類的名稱,能夠使用targetClass 屬性設置該名稱。也能夠經過設置bindingName 屬性來設置綁定名稱。在下面的示例中,咱們綁定Flights 類:

<beans> <bean id="jibxFlightsMarshaller" class="org.springframework.oxm.jibx.JibxMarshaller"> <property name="targetClass">org.springframework.oxm.jibx.Flights</property> </bean> ...</beans>

JibxMarshaller 是爲單個類配置的。若是要Marshaller多個類,則必須使用不一樣的targetClass 屬性值配置多個JibxMarshaller 實例。


XML配置命名空間

jibx-marshaller標記配置org.springframework.oxm.jibx.JibxMarshaller,以下例所示:

<oxm:jibx-marshaller id="marshaller" target-class="org.springframework.ws.samples.airline.schema.Flight"/>

下表描述了可用的屬性:

屬性

描述

是否須要

id

Marshaller的id

No

target-class

Marshaller的目標類

Yes

bindingName

Marshaller使用的綁定名稱

No


XStream

    XStream是一個簡單的庫,用於將對象序列化爲XML並再次序列化。它不須要任何映射並生成乾淨的XML。Spring集成類位於org.springframework.oxm.xstream包中。


使用XStreamMarshaller

      XStreamMarshaller不須要任何配置,能夠直接在應用程序上下文中配置。要進一步自定義XML,能夠設置別名映射,該映射由映射到類的字符串別名組成,以下例所示:


<beans> <bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"> <property name="aliases"> <props> <prop key="Flight">org.springframework.oxm.xstream.Flight</prop> </props> </property> </bean> ...</beans>

注意:默認狀況下,XStream容許對任意類進行解組,這可能致使不安全的Java序列化效果。所以,咱們不建議使用XStreamMarshaller從外部源(即Web)解析XML,由於這可能會致使安全漏洞。

     若是選擇使用XStreamMarshaller從外部源解析XML,請在XStreamMarshaller上設置supportedClasses屬性,以下例所示:

<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"> <property name="supportedClasses" value="org.springframework.oxm.xstream.Flight"/> ...</bean>

這樣作能夠確保只有註冊的類纔有資格進行解析。

     此外,你能夠註冊自定義轉換器,以確保只有受支持的類才能被解析。除了明確支持應該支持的域類的轉換器以外,可能還但願添加CatchAllConverter做爲列表中的最後一個轉換器。所以,不會調用優先級較低且可能存在安全漏洞的默認XStream轉換器。



     今天爲止關於Spring數據訪問內容所有講完。共34講,至少6萬字。重點講述事物相關內容。

    接下來寫關於Spring基於Servlet  API構建並部署到Servlet容器的Servlet堆棧web應用程序的支持。單獨的介紹包括Spring MVC、View技術、CORS支持和WebSocket支持等。


歡迎關注和轉發Spring中文社區(加微信羣,能夠關注後加我微信):





本文分享自微信公衆號 - Spring中文社區(gh_81d233bb13a4)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索