Search code examples
javaspringspring-bootdozer

How to hint dozer to copy autowired beans instead of cloning?


I have class

public class Monitor {

    @Autowired
    private QueryService;


    public void perform(){
       // code here
    } 
}

I am using dozer but dozer is giving me exception like

org.dozer.MappingException: java.lang.NoSuchMethodException:XX.QueryService.<init>()
        at org.dozer.util.MappingUtils.throwMappingException(MappingUtils.java:82) ~[dozer-core-6.0.0.jar:6.0.0]
        at org.dozer.factory.ConstructionStrategies$ByConstructor.newInstance(ConstructionStrategies.java:327) ~[dozer-core-6.0.0.jar:6.0.0]
        at org.dozer.factory.ConstructionStrategies$ByConstructor.create(ConstructionStrategies.java:311) ~[dozer-core-6.0.0.jar:6.0.0]
        at org.dozer.factory.DestBeanCreator.applyStrategies(DestBeanCreator.java:80) ~[dozer-core-6.0.0.jar:6.0.0]
        at org.dozer.factory.DestBeanCreator.create(DestBeanCreator.java:65) ~[dozer-core-6.0.0.jar:6.0.0]
        at org.dozer.MappingProcessor.createByCreationDirectiveAndMap(MappingProcessor.java:220) ~[dozer-core-6.0.0.jar:6.0.0]
        at org.dozer.MappingProcessor.mapCustomObject(MappingProcessor.java:548) ~[dozer-core-6.0.0.jar:6.0.0]
        at org.dozer.MappingProcessor.mapOrRecurseObject(MappingProcessor.java:501) ~[dozer-core-6.0.0.jar:6.0.0]
        at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:392) ~[dozer-core-6.0.0.jar:6.0.0]
        at org.dozer.MappingProcessor.mapField(MappingProcessor.java:338) ~[dozer-core-6.0.0.jar:6.0.0]
        at org.dozer.MappingProcessor.map(MappingProcessor.java:298) ~[dozer-core-6.0.0.jar:6.0.0]

Is there any way to hint dozer that this is java bean and use existing bean i.e. copy the bean as it is instead of cloning it?


Solution

  • From Dozer documentation:

    Dozer supports copying an object by reference. No conversion/transformation is done for such objects. This approach allows to decrease a number of object allocations, but is applicable only when Java Beans are to be thrown away (Garbage Collected) after transformation. This approach is generally recommended for performance tuning of the mapping process when possible. Make sure that both object types are the same or you will run into casting problems. The default value is 'false'.

    <field copy-by-reference="true">
      <a>copyByReference</a>
      <b>copyByReferencePrime</b>
    </field>