Fork me on GitHub

Event Listening

By implementing the DozerEventListener interface dozer allows you to listen to 4 different events:

  • mappingStarted
  • mappingFinished
  • preWritingDestinationValue
  • postWritingDestinationValue

A DozerEvent object is passed into these callback methods which stores information about the ClassMap, FieldMap, Source object, destination object, and destination value. This will allow you to extend dozer and manipulate mapped objects at run-time. The interface is shown below:


         
public interface DozerEventListener {
  public void mappingStarted(DozerEvent event);
  public void preWritingDestinationValue(DozerEvent event);
  public void postWritingDestinationValue(DozerEvent event);
  public void mappingFinished(DozerEvent event);
}        
        
      

The listeners that you create can be injected to the DozerBeanMapper using an IOC like Spring or set directly on your DozerBeanMapper instance by using the setEventListeners() method. Below is an example using Spring to inject an event listener:

         
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
          
<beans default-lazy-init="false">
          
 <bean id="EventMapper" class="org.dozer.DozerBeanMapper">
  <property name="mappingFiles">
   <list>
    <value>dozerBeanMapping.xml</value>
   </list>
  </property>
  <property name="eventListeners">
   <list>
    <ref bean="eventTestListener"/>
   </list>
  </property>
 </bean>
          
 <bean id="eventTestListener" 
       class="org.dozer.event.EventTestListener"/>
            
</beans>