Fork me on GitHub

General Usage

Requirements for running Dozer:

  • Dozer uses SLF4J for logging purposes.
  • Dozer needs a few third-party runtime jars.
  • All of the required runtime jars are in the {dozer.home}/repository directory and need to be in your Classpath
  • The dozer.jar file in the {dozer.home}/dist directory needs to be in your Classpath

Dozer Bean Mapper

Before we go over setting up custom xml bean mappings, let us look at a simple example of using Dozer. The Dozer mapping implementation 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.

          
Mapper mapper = new DozerBeanMapper();

DestinationObject destObject = 
    mapper.map(sourceObject, DestinationObject.class);
             or
DestinationObject destObject = new DestinationObject();
mapper.map(sourceObject, destObject);
          
        

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 Mapper as singleton="true", a DozerBeanMapperSingletonWrapper convenience class has been provided in the Dozer jar.

Dozer operates in two general modes: implicit and explicit. Implicit mode is activated by default and tries to resolve mappings for you. It uses simple assumptions that if two objects are passed for mapping then bean properties with the same names should be mapped. If there are additional mappings needed, which can not be derived by the naming you should add those either via Xml, Annotations or API.

Explicit mode assumes that no mappings should be performed or "guessed" until you have specified those specifically. The amount of coding is higher in explicit mode, but sometimes you would like to have full control on what is going on during the mappings process and this approach is also used in many of the productive applications. Implicit/Explicit mapping switch is called "wildcard" in Dozer. Whenever you encounter that in configuration you know what behavior to expect from now on.

Injecting Custom Mapping Files

The Dozer mapping xml file(s) define any custom mappings that can't be automatically performed by the Dozer mapping engine. Any custom Dozer mapping files need to be injected into the Mapper implementation(org.dozer.DozerBeanMapper). Both setter-based and constructor-based injection are supported.

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 programmatically. 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 = 
    mapper.map(sourceObject, DestinationObject.class);
          
          
IMPORTANT: Mapper instance(s) should be setup as a Singleton regardless of how you choose to inject the Mapper instance(s). You should configure the Mapper 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 inefficient and unnecessary. The DozerBeanMapper.java class is thread safe.

Spring Integration

The following is an example how the Mapper bean would be configured via Spring.
Sample spring.xml bean definition...

             
<bean id="mapper" class="org.dozer.DozerBeanMapper">
  <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>
          
          

Dozer Bean Mapper Singleton Wrapper

There is one way to configure the DozerBeanMapperSingletonWrapper to use your custom mapping file.

  • Using one mapping file: A file called dozerBeanMapping.xml file will be loaded if it is in your Classpath. You can find a sample of this file in the {dozer.home}/mappings directory.

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.

Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
DestinationObject destObject = 
    mapper.map(sourceObject, DestinationObject.class);

or

Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
DestinationObject destObject = new DestinationObject();
mapper.map(sourceObject, destObject);