Is there a way to define a custom converter in Dozer for converting one top-level type to another, which is itself a Spring bean and thus can get its dependencies injected?
The dozer docs propose to add the following XML definition:
<converter type="org.dozer.converters.TestCustomConverter" >
<class-a>org.dozer.vo.CustomDoubleObject</class-a>
<class-b>java.lang.Double</class-b>
</converter>
Unfortunately, this causes Dozer to instantiate org.dozer.converters.TestCustomConverter
directly, which will skip dependency injection. Is there a way to reference a Spring bean instead?
If your custom converter is a spring bean then the property 'customConvertersWithIds' of Dozer bean mapper can be used to refer to the converter spring bean. Then use this id to refer to the custom converter in the mappings. Here is how I made it work for me:
<bean id="dozerMapper" class="org.dozer.DozerBeanMapper" scope="singleton">
<property name="mappingFiles">
<list>
<value><mapping-file-name1></value>
<value><mapping-file-name2></value>
</list>
</property>
<property name="customConvertersWithId">
<map>
<entry key="crbConverter" value-ref="loadableFooBeanConverter"/>
<entry key="sbConverter" value-ref="loadableXyzBeanConverter"/>
</map>
</property>
</bean>
I have the converter classes annotated, e.g., @component("loadableFooBeanConverter")
An example of a mapping:
<mapping>
<class-a>${Abc}</class-a>
<class-b>${AbcBean}</class-b>
<field custom-converter-id="sbConverter">
<a>XyzId</a>
<b>Xyz</b>
<b-hint>${XyzBean}</b-hint>
</field>
</mapping>