Spring Framework Integration

We provide the dozer-spring4 for integrating spring application. If you are using Apache Maven, simply copy-paste this dependency to your project.



Add the DozerBeanMapperFactoryBean to your Spring configuration file. The mappingFiles property is where you should specify any custom dozer mapping files that you have created. This list can be empty if you don’t have any custom mappings. It is also possible to set custom event listeners and bean factories.


You should define the Dozer mapper bean is defined as singleton="true". You should configure the Mapper instance(s) this way so that you do not have to reload and reinitialize the mapping files for each individual mapping during the lifecycle of your app. Reinitializing the mapping files for each mapping would be inefficient and unnecessary. The DozerBeanMapper class is thread safe.

XML based configuration

Note that this Factory Bean supports Spring Resources, which means that you could load mapping Xml files by classpath mask for example.

<bean id="dozerMapper" class="com.github.dozermapper.spring.DozerBeanMapperFactoryBean">
    <property name="mappingFiles" value="classpath*:/*mapping.xml" />
    <property name="customConverters">
            <bean class="com.github.dozermapper.core.converters.CustomConverter" />
    <property name="eventListeners">
            <bean class="com.github.dozermapper.core.listeners.EventListener" />
    <property name="factories">
            <entry key="id" value-ref="bean-factory-ref" />

Alternatively, you can define the DozerBeanMapper using the dozer name space as follow:

<beans xmlns="http://www.springframework.org/schema/beans"

    <dozer:mapper id="dozerMapper"/>


Java based configuration

When using Java configuration, bean definition is as follows:

public class DozerConfig {
    public DozerBeanMapperFactoryBean dozerMapper(
        ResourcePatternResolver resourcePatternResolver) throws IOException {
        DozerBeanMapperFactoryBean factoryBean = new DozerBeanMapperFactoryBean();
        // ...
        return factoryBean;

How to use in your application

Using Spring to retrieve the Dozer Mapper…​…​

Look-up based usage

You can use the Dozer Mapper that looked up from the Spring ApplicationContext(BeanFactory).

Mapper dozerMapper = applicationContext.getBean("dozerMapper", Mapper.class);
DestinationObject destObject = dozerMapper.map(sourceObject, DestinationObject.class);
Injection based usage

Alternatively, you can use the Dozer Mapper that injected to your component by Spring DI container.

public class YourComponent {
    private final Mapper dozerMapper;
    public YourComponent(Mapper dozerMapper) {
        this.dozerMapper = dozerMapper;
    public void anyMethod() {
        // ...
        DestinationObject destObject =
            dozerMapper.map(sourceObject, DestinationObject.class);
        // ...

How to use in Spring Boot Application

Please see Spring Boot Integration section.

results matching ""

    No results matching ""