此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
使用对象 XML 映射器封送 XML
介绍
本章描述了 Spring 的 Object-XML Mapping 支持。对象 XML 映射(简称 O-X 映射)是将 XML 文档相互转换的过程 一个对象。此转换过程也称为 XML 封送或 XML 序列化。本章可以互换使用这些术语。
在 O-X 映射领域中,编组器负责序列化 object (graph) 转换为 XML。以类似的方式,解组器将 XML 反序列化为 对象图。此 XML 可以采用 DOM 文档、输入或输出的形式 stream 或 SAX 处理程序。
使用 Spring 满足您的 O/X 映射需求的一些好处是:
易于配置
Spring 的 bean factory 使配置编组器变得容易,而无需 构造 JAXB 上下文、JiBX 绑定工厂等。您可以配置封送处理程序 就像您在应用程序上下文中的任何其他 bean 一样。此外,基于 XML 命名空间 配置可用于许多编组器,使配置均匀 简单。
一致的接口
Spring 的 O-X 映射通过两个全局接口运行:Marshaller
和Unmarshaller
.这些抽象允许您切换 O-X 映射框架
相对容易,只需对执行
编组。这种方法还有一个额外的好处,就是可以执行 XML
使用混合匹配方法进行封送(例如,使用 JAXB 执行一些封送处理
还有一些由 XStream 提供),让您使用每个
科技。
Marshaller
和Unmarshaller
如简介中所述,编组处理程序序列化对象 转换为 XML,并且解组器将 XML 流反序列化为对象。本节介绍 用于此目的的两个 Spring 接口。
理解Marshaller
Spring 抽象了org.springframework.oxm.Marshaller
接口,其主要方法如下:
public interface Marshaller {
/**
* Marshal the object graph with the given root into the provided Result.
*/
void marshal(Object graph, Result result) throws XmlMappingException, IOException;
}
这Marshaller
interface 有一个 main 方法,该方法将给定对象封送到
鉴于javax.xml.transform.Result
.结果是一个标记接口,基本上
表示 XML 输出抽象。具体实现包装各种 XML
表示形式,如下表所示:
结果实现 | 包装 XML 表示 |
---|---|
|
|
|
|
|
|
尽管marshal() method 接受一个 plain object 作为其第一个参数,则大多数Marshaller implementations 无法处理任意对象。相反,对象类
必须在映射文件中映射,使用注释进行标记,并使用
marshaller 或具有公共基类。请参阅本章后面的章节
来确定您的 O-X 技术如何管理这一点。 |
理解Unmarshaller
与Marshaller
,我们有org.springframework.oxm.Unmarshaller
接口,下面的清单显示了:
public interface Unmarshaller {
/**
* Unmarshal the given provided Source into an object graph.
*/
Object unmarshal(Source source) throws XmlMappingException, IOException;
}
这个接口还有一个方法,它从给定的javax.xml.transform.Source
(一个 XML 输入抽象)并返回读取的对象。如
跟Result
,Source
是一个具有三个具体实现的标记接口。每
包装不同的 XML 表示形式,如下表所示:
源实现 | 包装 XML 表示 |
---|---|
|
|
|
|
|
|
即使有两个单独的封送接口 (Marshaller
和Unmarshaller
),Spring-WS 中的所有实现都在一个类中实现两者。
这意味着您可以连接一个 marshaller 类,并将其同时称为
marshaller 并作为 unmarshaller in yourapplicationContext.xml
.
用Marshaller
和Unmarshaller
您可以将 Spring 的 OXM 用于各种情况。在下面的示例中,我们 使用它来将 Spring Management 的应用程序的设置编组为 XML 文件。在下面的示例中,我们 使用一个简单的 JavaBean 来表示设置:
-
Java
-
Kotlin
public class Settings {
private boolean fooEnabled;
public boolean isFooEnabled() {
return fooEnabled;
}
public void setFooEnabled(boolean fooEnabled) {
this.fooEnabled = fooEnabled;
}
}
class Settings {
var isFooEnabled: Boolean = false
}
application 类使用此 bean 来存储其设置。除了 main 方法之外,
class 有两种方法:saveSettings()
将设置 Bean 保存到名为settings.xml
和loadSettings()
再次加载这些设置。以下内容main()
方法
构造一个 Spring 应用程序上下文并调用以下两个方法:
-
Java
-
Kotlin
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
public class Application {
private static final String FILE_NAME = "settings.xml";
private Settings settings = new Settings();
private Marshaller marshaller;
private Unmarshaller unmarshaller;
public void setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
}
public void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
public void saveSettings() throws IOException {
try (FileOutputStream os = new FileOutputStream(FILE_NAME)) {
this.marshaller.marshal(settings, new StreamResult(os));
}
}
public void loadSettings() throws IOException {
try (FileInputStream is = new FileInputStream(FILE_NAME)) {
this.settings = (Settings) this.unmarshaller.unmarshal(new StreamSource(is));
}
}
public static void main(String[] args) throws IOException {
ApplicationContext appContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
Application application = (Application) appContext.getBean("application");
application.saveSettings();
application.loadSettings();
}
}
class Application {
lateinit var marshaller: Marshaller
lateinit var unmarshaller: Unmarshaller
fun saveSettings() {
FileOutputStream(FILE_NAME).use { outputStream -> marshaller.marshal(settings, StreamResult(outputStream)) }
}
fun loadSettings() {
FileInputStream(FILE_NAME).use { inputStream -> settings = unmarshaller.unmarshal(StreamSource(inputStream)) as Settings }
}
}
private const val FILE_NAME = "settings.xml"
fun main(args: Array<String>) {
val appContext = ClassPathXmlApplicationContext("applicationContext.xml")
val application = appContext.getBean("application") as Application
application.saveSettings()
application.loadSettings()
}
这Application
需要marshaller
以及一个unmarshaller
属性。我们
可以使用以下applicationContext.xml
:
<beans>
<bean id="application" class="Application">
<property name="marshaller" ref="xstreamMarshaller" />
<property name="unmarshaller" ref="xstreamMarshaller" />
</bean>
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/>
</beans>
此应用程序上下文使用 XStream,但我们可以使用任何其他编组处理程序
实例。请注意,默认情况下,XStream 不需要
任何进一步的配置,因此 bean 定义相当简单。另请注意,XStreamMarshaller
同时实现Marshaller
和Unmarshaller
,因此我们可以参考xstreamMarshaller
bean 中marshaller
和unmarshaller
属性的
应用。
此示例应用程序生成以下内容settings.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<settings foo-enabled="false"/>
XML 配置命名空间
您可以使用 OXM 命名空间中的标记更简洁地配置编组器。 要使这些标记可用,您必须首先在 XML 配置文件的序言。以下示例显示了如何执行此作:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm" (1)
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/oxm
https://www.springframework.org/schema/oxm/spring-oxm.xsd"> (2)
1 | 引用oxm 图式。 |
2 | 指定oxm Schema 位置。 |
该架构使以下元素可用:
每个标记在其各自的 marshaller's 部分中都有说明。不过,举个例子, JAXB2 编组器的配置可能类似于以下内容:
<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>
JAXB
JAXB 绑定编译器将 W3C XML Schema 转换为一个或多个 Java 类,即jaxb.properties
文件,可能还有一些资源文件。JAXB 还提供了一种
从带注释的 Java 类生成架构。
Spring 支持 JAXB 2.0 API 作为 XML 编组策略,遵循Marshaller
和Unmarshaller
中描述的接口Marshaller
和Unmarshaller
.
相应的集成类位于org.springframework.oxm.jaxb
包。
用Jaxb2Marshaller
这Jaxb2Marshaller
类实现了 Spring 的Marshaller
和Unmarshaller
接口。它需要一个上下文路径才能运行。您可以通过设置contextPath
财产。上下文路径是以冒号分隔的 Java 包的列表
包含架构派生类的名称。它还提供了一个classesToBeBound
财产
它允许您设置编组器要支持的类数组。图式
通过向 Bean 指定一个或多个模式资源来执行验证,如下例所示:
<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"/>
或者,您可以使用class-to-be-bound
子元素:
<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>
下表描述了可用属性:
属性 | 描述 | 必填 |
---|---|---|
|
封送处理程序的 ID |
不 |
|
JAXB 上下文路径 |
不 |
吉BX
JiBX 框架提供了一个类似于 Hibernate 为 ORM 提供的解决方案:一个 binding 定义定义如何将 Java 对象转换为此对象或从中转换的规则 在准备绑定并编译类之后,JiBX 绑定编译器 增强了类文件并添加了代码以处理类实例的转换 from 或 to XML。
有关 JiBX 的更多信息,请参阅 JiBX Web
站点。Spring 集成类位于org.springframework.oxm.jibx
包。
用JibxMarshaller
这JibxMarshaller
类实现Marshaller
和Unmarshaller
接口。要进行作,它需要要封送的类的名称,您可以
set 使用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
配置为单个类。如果要封送多个
类,您必须配置多个JibxMarshaller
具有不同targetClass
属性值。
XStream
XStream 是一个简单的库,用于将对象序列化为 XML,然后再序列化回来。它没有 需要任何映射并生成干净的 XML。
有关 XStream 的更多信息,请参阅 XStream
网站。Spring 集成类位于org.springframework.oxm.xstream
包。
用XStreamMarshaller
这XStreamMarshaller
不需要任何配置,可以在
application context 直接访问。要进一步自定义 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 序列化效果。因此,我们不建议使用 如果您选择使用
这样做可以确保只有已注册的类才有资格进行 unmarshalling。 此外,您还可以注册自定义
converters 来确保只有您支持的类可以解组。你可以
想要添加一个 |
请注意,XStream 是一个 XML 序列化库,而不是一个数据绑定库。 因此,它对命名空间的支持有限。因此,它相当不适合使用 在 Web 服务中。 |