Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dozer and Spring integration

Tags:

spring

dozer

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.

like image 875
Michael Laffargue Avatar asked Nov 10 '12 15:11

Michael Laffargue


1 Answers

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;
}
like image 103
Biju Kunjummen Avatar answered Sep 20 '22 13:09

Biju Kunjummen