public class Foo { public String getReferenceID(); public void setReferenceID(String id); public String getFilename(); public void setFilename(String fileName); } public class Bar { public String getRefID(); public void setRefID(String id); public String getFileName(); public void setFileName(String fileName); }
我希望能够以规范化的形式解决这些问题,以便我可以对它们进行多态处理,这样我就可以使用Apache BeanUtils来做这些事情,比如:
PropertyUtils.copyProperties(object1,object2);
很明显,为每个类写一个适配器将是微不足道的…
public class CanonicalizedBar implements CanonicalizedBazBean { public String getReferenceID() { return this.delegate.getRefID(); } // etc. }
但是我想知道有没有更广泛和更动态的东西?某物将需要一对多的属性名称等价地图,以及代理类,并生成适配器?
解决方法
Dozer is a Java Bean to Java Bean mapper that recursively copies data
from one object to another. Typically,these Java Beans will be of
different complex types.Dozer supports simple property mapping,complex type mapping,
bi-directional mapping,implicit-explicit mapping,as well as
recursive mapping. This includes mapping collection attributes that
also need mapping at the element level.Dozer not only supports mapping between attribute names,but also automatically converting between types. Most conversion scenarios are supported out of the Box,but Dozer also allows you to specify custom conversions via XML.