MapperIF mapper = new DozerBeanMapper();
DestinationObject destObject =
(DestinationObject) mapper.map(sourceObject, DestinationObject.class);
IMPORTANT: For real-world applications it is not recommended that you create a new instance of the Mapper each time you map objects. Typically a system will only have one DozerBeanMapper instance per VM. If you are not using an IOC framework where you can define the MapperIF as singleton="true", a DozerBeanMapperSingletonWrapper convenience class has been provided in the Dozer jar.
If the two different types of data objects that you are mapping contain any fields that don't share a common property name, you will need to add a class mapping entry to your custom mapping xml file. These mappings xml files are used at runtime by the Dozer mapping engine.
Dozer automatically performs any type conversion when copying the source field data to the destination field. The Dozer mapping engine is bi-directional, so if you wanted to map the destination object to the source object, you do not need to add another class mapping to the xml file.
IMPORTANT: Fields that are of the same name do not need to be specified in the mapping xml file. Dozer automatically maps all fields with the same property name from the source object into the destination object.
<mapping>
<class-a>yourpackage.yourSourceClassName</class-a>
<class-b>yourpackage.yourDestinationClassName</class-b>
<field>
<A>yourSourceFieldName</A>
<B>yourDestinationFieldName</B>
</field>
</mapping>
The complete Dozer mapping xml file would look like the following. The Custom Mappings section contains more information on mapping options that are available to you for more complex use cases.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mappings PUBLIC "-//DOZER//DTD MAPPINGS//EN"
"http://dozer.sourceforge.net/dtd/dozerbeanmapping.dtd">
<mappings>
<configuration>
<stop-on-errors>true</stop-on-errors>
<date-format>MM/dd/yyyy HH:mm</date-format>
<wildcard>true</wildcard>
</configuration>
<mapping>
<class-a>yourpackage.yourSourceClassName</class-a>
<class-b>yourpackage.yourDestinationClassName</class-b>
<field>
<A>yourSourceFieldName</A>
<B>yourDestinationFieldName</B>
</field>
</mapping>
other custom class mappings would go here.......
</mappings>
Add the MapperIF bean to your Spring configuration file. The mappingFiles property is where you would specify any custom dozer mapping files that you have created. This list can be empty if you didn't have any custom mappings.
<bean id="net.sf.dozer.util.mapping.MapperIF"
class="net.sf.dozer.util.mapping.DozerBeanMapper" singleton="true">
<property name="mappingFiles">
<list>
<value>dozer-global-configuration.xml</value>
<value>dozer-bean-mappings.xml</value>
</list>
</property>
</bean>
IMPORTANT: Notice that that the Dozer mapper bean is defined as singleton="true" . You should configure the Mapper instance(s) this way so that you do not have to reload and reinitialize the mapping files for each individual mapping during the lifecycle of your app. Reinitializing the mapping files for each mapping would be inefficent and unnecessary. The DozerBeanMapper.java class is thread safe.
Using Spring to retrieve the Dozer Mapper......
MapperIF mapper = YourSpringBeanFactory.getBean(MapperIF.class);
DestinationObject destObject =
(DestinationObject) mapper.map(sourceObject, DestinationObject.class);