Search code examples
springdozer

Dozer and Spring integration


EDIT : A new lib has been introduced which clarify the thing for new versions

Since version 5.5.0 Spring integration comes within additional module dozer-spring.


Hi there I'm relatively new to Dozer and Spring and a bit confused about how to put that in place.

From the dozer website : http://dozer.sourceforge.net/documentation/usage.html

Spring integration ...

<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>

Now from http://dozer.sourceforge.net/documentation/springintegration.html :

<bean class="org.dozer.spring.DozerBeanMapperFactoryBean">
    <property name="mappingFiles" value="classpath*:/*mapping.xml"/>
    <property name="customConverters">
        <list>
            <bean class="org.dozer.converters.CustomConverter"/>      
        </list>
    </property>
    <property name="eventListeners">
        <list>
            <bean class="org.dozer.listeners.EventListener"/>
        </list>
    </property>
    <property name="factories">
        <map>
            <entry key="id" value-ref="bean-factory-ref"/>
        </map>
    </property>
</bean>

So I'm not really sure which way I should use it. My objectives is to have a mapper object in my business classes that will convert Business Objects to DTO (and reversely). So I think it just should be a basic Dependency Injection ?

Thanks for any help.


Solution

  • Both are valid approaches, just inject this mapper as a dependency in the service class responsible for mapping, eg:

    @Service
    public class MyMappingService{
     @Autowired DozerBeanMapper dozerBeanMapper;
    }
    

    With DozerBeanMapperFactoryBean the approach along these lines should work:

    <bean class="org.dozer.spring.DozerBeanMapperFactoryBean">
    ....
    </bean>
    

    This returns a mapper instance, so just inject in a mapper type this way:

    @Service
    public class MyMappingService{
     @Autowired Mapper dozerBeanMapper;
    }