大家好,欢迎来到IT知识分享网。
什么是OXM?
OXM是Object-to-XML-Mapping的缩写,它是一个O/M-mapper,将java对象映射成XML数据,或者将XML数据映射成java对象。它类似 XML-Marshalling 或者是 XML-Serialization,并不是什么新技术。目前Spring framework 3.0引入了该特性。
现在让我们看看Spring的承诺:
- 易于配置
- 一致的接口
- 一致的异常层次结构
听起来很不错,因为没有喜欢杂乱的接口,繁琐的配置,现在就让我们来检验一下Spring OXM的设计思想,本文着重讲述OXM特性的使用方法。
OXM in Spring 3.0
我们以一个Maven2实例工程来进行说明。首先,创建一个pom.xml文件:
- <project>
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.unitedcoders.examples</groupId>
- <artifactId>SpringOxmExample</artifactId>
- <packaging>jar</packaging>
- <version>1.0.0-SNAPSHOT</version>
- <name>SpringOxmExample</name>
- <url>http://united-coders.com</url>
- <build>
- <finalName>SpringOxmExample</finalName>
- <plugins>
- <!– Compiler Plugin to compile on Java 1.6 –>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <compilerVersion>1.6</compilerVersion>
- <fork>true</fork>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <!– Properties –>
- <properties>
- <spring-version>3.0.0.RELEASE</spring-version>
- </properties>
- <!– Dependencies –>
- <dependencies>
- <!– Spring framework –>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-oxm</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <!– Castor Xml –>
- <dependency>
- <groupId>org.codehaus.castor</groupId>
- <artifactId>castor</artifactId>
- <version>1.2</version>
- </dependency>
- <dependency>
- <groupId>xerces</groupId>
- <artifactId>xercesImpl</artifactId>
- <version>2.9.1</version>
- </dependency>
- <!– Logging –>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1.1</version>
- </dependency>
- <!– Test dependencies –>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.4</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
- <project>
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.unitedcoders.examples</groupId>
- <artifactId>SpringOxmExample</artifactId>
- <packaging>jar</packaging>
- <version>1.0.0-SNAPSHOT</version>
- <name>SpringOxmExample</name>
- <url>http://united-coders.com</url>
- <build>
- <finalName>SpringOxmExample</finalName>
- <plugins>
- <!– Compiler Plugin to compile on Java 1.6 –>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <compilerVersion>1.6</compilerVersion>
- <fork>true</fork>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <!– Properties –>
- <properties>
- <spring-version>3.0.0.RELEASE</spring-version>
- </properties>
- <!– Dependencies –>
- <dependencies>
- <!– Spring framework –>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-oxm</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <!– Castor Xml –>
- <dependency>
- <groupId>org.codehaus.castor</groupId>
- <artifactId>castor</artifactId>
- <version>1.2</version>
- </dependency>
- <dependency>
- <groupId>xerces</groupId>
- <artifactId>xercesImpl</artifactId>
- <version>2.9.1</version>
- </dependency>
- <!– Logging –>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1.1</version>
- </dependency>
- <!– Test dependencies –>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.4</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
该实例中我用到了三个spring 模块,spring-core,spring-beans以及spring-oxm。另外,我使用Castor工程和Apache Xerces作为XML-Marshalling-dependencies.
接着,创建一个简单的javabean,将被序列换成XML字符串:
- package com.unitedcoders.examples.spring.oxm.beans;
- public class Person {
- private String firstname;
- private String lastname;
- private boolean developer;
- public String getFirstname() {
- return firstname;
- }
- public void setFirstname(String firstname) {
- this.firstname = firstname;
- }
- public String getLastname() {
- return lastname;
- }
- public void setLastname(String lastname) {
- this.lastname = lastname;
- }
- public boolean isDeveloper() {
- return developer;
- }
- public void setDeveloper(boolean developer) {
- this.developer = developer;
- }
- }
- package com.unitedcoders.examples.spring.oxm.beans;
- public class Person {
- private String firstname;
- private String lastname;
- private boolean developer;
- public String getFirstname() {
- return firstname;
- }
- public void setFirstname(String firstname) {
- this.firstname = firstname;
- }
- public String getLastname() {
- return lastname;
- }
- public void setLastname(String lastname) {
- this.lastname = lastname;
- }
- public boolean isDeveloper() {
- return developer;
- }
- public void setDeveloper(boolean developer) {
- this.developer = developer;
- }
- }
然后,为O/X-Mapper创建一个接口:
- package com.unitedcoders.examples.spring.oxm.mapper;
- import java.io.IOException;
- public interface OxMapper {
- /
- * Serializes assigned Object into a file with the assigned name.
- *
- * @param object
- * – Object that should be serialized
- * @param filename
- * – name of the XML-file
- * @throws IOException
- */
- public abstract void writeObjectToXml(Object object, String filename) throws IOException;
- /
- * Deserializes an object from the assigned file.
- *
- * @param filename
- * – name of the file that should be deserialized
- * @return deserialized object
- * @throws IOException
- */
- public abstract Object readObjectFromXml(String filename) throws IOException;
- }
- package com.unitedcoders.examples.spring.oxm.mapper;
- import java.io.IOException;
- public interface OxMapper {
- /
- * Serializes assigned Object into a file with the assigned name.
- *
- * @param object
- * – Object that should be serialized
- * @param filename
- * – name of the XML-file
- * @throws IOException
- */
- public abstract void writeObjectToXml(Object object, String filename) throws IOException;
- /
- * Deserializes an object from the assigned file.
- *
- * @param filename
- * – name of the file that should be deserialized
- * @return deserialized object
- * @throws IOException
- */
- public abstract Object readObjectFromXml(String filename) throws IOException;
- }
我定义了两个方法,一个将对象转化成xml文件,一个将xml文件转换成对象。以下是该接口的实现类:
- package com.unitedcoders.examples.spring.oxm.mapper;
- 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.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.oxm.Marshaller;
- import org.springframework.oxm.Unmarshaller;
- import org.springframework.oxm.XmlMappingException;
- public class OxMapperImpl implements OxMapper {
- private static final Log LOG = LogFactory.getLog(OxMapperImpl.class);
- private Marshaller marshaller;
- private Unmarshaller unmarshaller;
- public void writeObjectToXml(Object object, String filename) throws IOException {
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(filename);
- marshaller.marshal(object, new StreamResult(fos));
- } catch (XmlMappingException e) {
- LOG.error(“Xml-Serialization failed due to an XmlMappingException.”, e);
- } catch (IOException e) {
- LOG.error(“Xml-Serialization failed due to an IOException.”, e);
- } finally {
- if (fos != null) {
- fos.close();
- }
- }
- }
- public Object readObjectFromXml(String filename) throws IOException {
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(filename);
- return unmarshaller.unmarshal(new StreamSource(fis));
- } catch (IOException e) {
- LOG.error(“Xml-Deserialization failed due to an IOException.”, e);
- } finally {
- if (fis != null) {
- fis.close();
- }
- }
- return null;
- }
- public Marshaller getMarshaller() {
- return marshaller;
- }
- public void setMarshaller(Marshaller marshaller) {
- this.marshaller = marshaller;
- }
- public Unmarshaller getUnmarshaller() {
- return unmarshaller;
- }
- public void setUnmarshaller(Unmarshaller unmarshaller) {
- this.unmarshaller = unmarshaller;
- }
- }
- package com.unitedcoders.examples.spring.oxm.mapper;
- 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.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.oxm.Marshaller;
- import org.springframework.oxm.Unmarshaller;
- import org.springframework.oxm.XmlMappingException;
- public class OxMapperImpl implements OxMapper {
- private static final Log LOG = LogFactory.getLog(OxMapperImpl.class);
- private Marshaller marshaller;
- private Unmarshaller unmarshaller;
- public void writeObjectToXml(Object object, String filename) throws IOException {
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(filename);
- marshaller.marshal(object, new StreamResult(fos));
- } catch (XmlMappingException e) {
- LOG.error(“Xml-Serialization failed due to an XmlMappingException.”, e);
- } catch (IOException e) {
- LOG.error(“Xml-Serialization failed due to an IOException.”, e);
- } finally {
- if (fos != null) {
- fos.close();
- }
- }
- }
- public Object readObjectFromXml(String filename) throws IOException {
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(filename);
- return unmarshaller.unmarshal(new StreamSource(fis));
- } catch (IOException e) {
- LOG.error(“Xml-Deserialization failed due to an IOException.”, e);
- } finally {
- if (fis != null) {
- fis.close();
- }
- }
- return null;
- }
- public Marshaller getMarshaller() {
- return marshaller;
- }
- public void setMarshaller(Marshaller marshaller) {
- this.marshaller = marshaller;
- }
- public Unmarshaller getUnmarshaller() {
- return unmarshaller;
- }
- public void setUnmarshaller(Unmarshaller unmarshaller) {
- this.unmarshaller = unmarshaller;
- }
- }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/148202.html