Experience Spring's object/xml mapping support

Source: Internet
Author: User

Common abbreviations

API: Application Programming Interface

IDE: Integrated development environment

XML: Extensible Markup Language

What is Spring?

Spring is a robust Java application framework developed by Rod Johnson and has been widely used in the Java development community. Spring is famous for its popular technical vocabulary, such as "Dependency injection (Dependency injection)", "inversion control inversion" and "aspect-oriented Programming (aspect-oriented programming) "and so on. It also supports the Model-view-controller (MVC) pattern, which is well compatible with the various database drivers for data access. In addition, it supports transaction processing, unit testing, batch processing, and security.

Given Spring's good reputation and long history, it is often the preferred framework for rapid application development. But perhaps the biggest benefit is that Spring is free.

What is o/x Mapper?

A new feature of Spring 3.0 is o/x Mapper. The concept of o/x mapper is not new, and O represents object,x for XML. Its purpose is to convert back and forth between Java objects (almost always a plain old Java object, or abbreviated as POJO) and XML documents.

For example, you might have a simple bean with several attributes, and your business needs to convert that Java object into an XML document. Spring's o/x Mapper can solve that problem for you. If, in turn, you need to convert an XML document into a simple Java bean,spring o/x Mapper is also competent.

One thing to note: The Spring o/x Mapper just defines a unified interface that is implemented by a popular third party framework. To take advantage of the Spring's o/x functionality, you need a utility that converts between Java objects and XML back and forth. Castor is such a popular third party tool that this article will use this tool. Other such tools include XMLBeans, Java architecture for XML Binding (JAXB), JiBX, and XStream.

Marshalling and reconciliation Group

When you make a o/x mapping, you often see the terms grouping (marshalling) reconciliation Group (unmarshalling).

Grouping refers to the process of converting a Java bean into an XML document, which means that all the field and field values of the Java bean are populated as XML elements or attributes into an XML file. Sometimes, grouping is also called serialization (serializing).

As you would expect, a solution group is a completely different process from grouping an XML document to a Java bean, which means that all elements or attributes of an XML document are populated in Java beans as Java fields. Sometimes, a group of solutions is also called deserialization (deserializing).

Benefits of using Spring's o/x Mapper

One of the most immediate benefits of using spring's o/x Mapper is that you can simplify configuration by leveraging other features of the spring framework. Spring's bean library supports an object that uses those marshalling objects by injecting the instantiated o/x marshalling (that is, "dependency injection" mentioned earlier). Again, this will accelerate application development and deployment.

Following solid object-oriented design practices, the Spring o/x framework defines only two interfaces: Marshaller and Unmarshaller, which are used to perform o/x functions, which is another significant benefit of using this framework. The implementations of these interfaces are completely open to independent developers, and developers can easily switch them without modifying the code. For example, if you start using Castor for o/x transformations, but then find that it lacks a feature that you need, you can switch to XMLBeans without any code changes. The only thing you need to do is change the Spring configuration file to use the new o/x framework.

Another benefit of using Spring's o/x Mapper is a unified exception hierarchy. The spring framework follows the pattern established using its data access module by wrapping the original exception object into the Run-time exception that Spring itself created for o/x Mapper. Because the original exception thrown by a third-party provider is wrapped into a Spring run-time exception, you can pinpoint the root cause of the exception. You don't have to bother modifying your code to catch exceptions because the exception is wrapped in a run-time exception. The following several run-time exceptions extend the underlying exception xmlmappingexception:genericmarshallingfailureexception, validationfailureexception, Marshallingfailureexception and Unmarshallingfailureexception.

A simple Demo

Now that you know the background and basics of Spring's o/x Mapper, you can examine how it is used. In this article, you first create a simple Spring application that is independent of any Java Enterprise dependencies. You then create a simple Java class that accesses the Spring's configuration file to instantiate the class and inject o/x dependencies. See the Downloads section for links to all source code files, including configuration files.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.