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源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索