Dozer and Spring integration

给你一囗甜甜゛ 提交于 2020-01-01 03:19:07

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/13323282/dozer-and-spring-integration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!