Requirements for running Dozer:
MapperIF mapper = new DozerBeanMapper();
DestinationObject destObject =
(DestinationObject) mapper.map(sourceObject, DestinationObject.class);
or
DestinationObject destObject = new DestinationObject();
mapper.map(sourceObject, destObject);
Preferably, you will be using an IOC framework such as Spring for these Dozer injection requirements.
Alternatively, the injection of mapping files can be done programatically. Below is a programmatic
approach to creating a bean mapper. Note that this is
NOT the recommended way to retrieve the bean mapper
. Each new instance needs to be initialized and this consumes time as well as resources. If you are using
the mapper this way just wrap it using the singleton pattern.
List myMappingFiles = new ArrayList();
myMappingFiles.add("dozerBeanMapping.xml");
myMappingFiles.add("someOtherDozerBeanMappings.xml");
DozerBeanMapper mapper = new DozerBeanMapper();
mapper.setMappingFiles(myMappingFiles);
DestinationObject destObject = (DestinationObject) mapper.map(sourceObject, DestinationObject.class);
The following is an example how the Mapper bean would be configured via Spring.
Sample spring.xml bean definition...
<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>
<value>more-dozer-bean-mappings.xml</value>
</list>
</property>
</bean>
There is one way to configure the DozerBeanMapperSingletonWrapper to use your custom mapping file.
The mapping file defines all of the relationships between Java classes and their attributes. The Custom Mappings section details the custom XML mapping options that are available.
The following example show how to use the DozerBeanMapperSingletonWrapper. Dozer has a method called map which takes a source object and either a destination object or destination object class type. After mapping the two objects it then returns the destination object with all of its mapped fields.
MapperIF mapper = DozerBeanMapperSingletonWrapper.getInstance(); DestinationObject destObject =
(DestinationObject) mapper.map(sourceObject, DestinationObject.class);
or
MapperIF mapper = DozerBeanMapperSingletonWrapper.getInstance(); DestinationObject destObject = new
DestinationObject(); mapper.map(sourceObject, destObject);