@ -1464,16 +1464,16 @@ public class MyController {
@@ -1464,16 +1464,16 @@ public class MyController {
<sectionid="org.springframework.mapping">
<title>Spring 3 Object Mapping</title>
<para>
There are scenarios, particularly in large message-oriented business applications, where data and object transformation is required.
There are scenarios, particularly in large message-oriented business applications, where object transformation is required.
For example, consider a complex Web Service where there is a separation between the data exchange model and the internal domain model used to structure business logic.
In cases like this, a general-purpose data mapping facility can be useful for automating the mapping between these disparate models.
In cases like this, a general-purpose object-to-object mapping facility can be useful for automating the mapping between these disparate models.
Spring 3 introduces such a facility built on the <linklinkend="expressions-intro">Spring Expression Language</link> (SpEL).
This facility is described in this section.
</para>
<sectionid="mapping-Mapping-API">
<title>Mapper API</title>
<para>
The API to implement data mapping logic is simple and strongly typed:
The API to implement object mapping logic is simple and strongly typed:
</para>
<programlistinglanguage="java"><![CDATA[
package org.springframework.mapping;
@ -1512,20 +1512,22 @@ public class PersonDtoPersonMapper implements Mapper<PersonDto, Person> {
@@ -1512,20 +1512,22 @@ public class PersonDtoPersonMapper implements Mapper<PersonDto, Person> {
A general purpose object Mapper implementation exists in the <classname>org.springframework.mapping.support</classname> package named <classname>SpelMapper</classname>.
Built on the flexible Spring Expression Language (SpEL), this Mapper is capable of mapping between objects of all types, including JavaBeans, Arrays, Collections, and Maps.
It is also extensible and allows additional MappableTypes to be configured.
A general purpose object-to-object mapping system exists in the <classname>org.springframework.mapping.support</classname> package.
Built on the flexible Spring Expression Language (SpEL), this system is capable of mapping between a variety of object types, including JavaBeans, Arrays, Collections, and Maps.
It can perform field-to-field, field-to-multi-field, and multi-field to field mappings.
It also can carry out type conversion and recursive mapping, often needed with rich object models.
</para>
<sectionid="mapping.SpelMapper-usage">
<title>Usage</title>
<para>
To use a SpelMapper with its default configuration, simply construct one and call map:
To obtain a general purpose object Mapper with its default configuration, simply call MappingFactory.getDefaultMapper().
Then invoke the Mapper by calling its <literal>map(Object, Object)</literal> operation:
By default, SpelMapper will map the fields on the source and target that have the same names.
By default, the defaultMapper will map the fields on the source and target that have the same names.
If the field types differ, the mapping system will attempt a type conversion using Spring 3's <linklinkend="core.convert">type conversion system</link>.
Nested bean properties are mapped recursively.
Any mapping failures will trigger a MappingException to be thrown.
@ -1562,7 +1564,7 @@ public class Account {
@@ -1562,7 +1564,7 @@ public class Account {
}
}]]></programlisting>
<para>
Used in the following test case:
Now used in the following test case:
</para>
<programlistinglanguage="java"><![CDATA[
@Test
@ -1577,8 +1579,7 @@ public void testDefaultSpelMappingBehavior() {
@@ -1577,8 +1579,7 @@ public void testDefaultSpelMappingBehavior() {
@ -1597,23 +1598,33 @@ public void testDefaultSpelMappingBehavior() {
@@ -1597,23 +1598,33 @@ public void testDefaultSpelMappingBehavior() {
<sectionid="mapping.SpelMapper-Explicit">
<title>Registering Explicit Mappings</title>
<para>
When default mapping rules are not sufficient, explicit mapping rules can be registered by calling one of the <literal>mapper.addMapping(...)</literal> method variants.
When default mapping rules are not sufficient, explicit mapping rules can be registered by obtaining a <classname>MapperBuilder</classname> and using it to construct a <classname>Mapper</classname>.
Explicit mapping rules always override the default.
For example, suppose you need to map <literal>AccountDto.name</literal> to <literal>Account.fullName</literal>.
Since the two property names are not the same, default auto-mapping would never be performed.
Handle a situation like this by explicitly registering a mapping rule: