Menu

#168 Add support for Spring resources

Dozer v5.2
closed
5
2010-01-02
2009-01-26
Andor
No

It would be nice to add spring resource support to the dozer bean mapper.
In my current project, Dozer is configured with a mapping-file pattern and a simple Spring resource adapter.
This gives you the possibility to find all dozer mapping resources on the classpath, which greatly improves
the testability of your project.

I use the following code to achieve this flexibility (checkout the line <property name="locations" value="classpath*:/dozer*.xml"/>)

spring-assembler.xml

<code>
<bean class="net.sf.dozer.util.mapping.DozerBeanMapper">
<property name="mappingFiles">
<bean class="xxx.SpringResourceList">
<property name="locations" value="classpath*:/dozer*.xml"/>
</bean>
</property>
</bean>

</beans>
</code>

This is the little adapter, which could go into DozerBeanMapper or a little spring wrapper class

public class SpringResourceList
extends ArrayList<String> {

public void setLocations(final org.springframework.core.io.Resource[] resources) throws IOException {
for (Resource resource : resources) {
add(resource.getURL().toExternalForm());
}
}
}

Best regards,
Andor

Discussion

  • ed

    ed - 2009-01-30

    I don't think this is really necessary as you should wrap the dozer mapping anyway such that you can inject different Dto mappers of other companies (Service pattern).
    The wrapper can do the above task for you.
    So you would get something like this:

    <bean id="dozerConverter" class="XXX.intern.impl.DtoConverter">
    <property name="mapper">
    <bean class="net.sf.dozer.util.mapping.DozerBeanMapper">
    <property name="mappingFiles">
    <list>
    <value>dozer/dozerGwtMapping.xml</value>
    </list>
    </property>
    </bean>
    </property>
    <property name="mappings">
    <map>
    <entry key="XXX.domain.Member" value="XXX.domain.MemberDto" />
    <entry key="XX.domain.Taxer$TaxerDefault" value="XX.domain.TaxerDto" />
    ...
    ... etc...

    In the above:
    - DtoConverter implements my general Dto converter.
    - In my Dto converter I also have the method map(Object obj). Note that the target class isn't specified and isn't needed as the Spring config contains the mapping for every type of object. The implementation simple looksup the target class and passes it on to dozer...

    That's it and it's a nice way of hdding the Dozer mapping out of your application which is basically what you want.

    Regards,
    Ed Bras

     
  • Andor

    Andor - 2009-01-30

    You got it - zhat is exactly what I do.

    The point i was talking about is the possibility to configure Dozer with a mapping file pattern like dozer-*.xml.
    But you are right the feature is useful only for people who do not know how to handle the spring internas.

    Maybe an articel/blog entry could do the same. I use the wrapper for generics, too:

    public <X> X assemble(final Class<X> destClass, final Object src) {
    return destClass.cast(mapper.map(src, destClass));
    }

    But thanks for your reply,

    Best regards,
    Andor

     
  • dmitry (lv)

    dmitry (lv) - 2009-08-04
    • milestone: 897287 --> Dozer v5.2
    • assigned_to: nobody --> buzdin
     
  • dmitry (lv)

    dmitry (lv) - 2010-01-02
    • status: open --> closed
     
  • dmitry (lv)

    dmitry (lv) - 2010-01-02

    We actually do support Resource configuration in DozerBeanMapperFactoryBean, provided to ease Spring integration. I have added explicit documentation in JavaDoc and updated Functional Tests to use the functionality.

     

Log in to post a comment.