规范化Java bean属性名称

前端之家收集整理的这篇文章主要介绍了规范化Java bean属性名称前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一堆第三方 Java类,它们使用不同的属性名称来实现相同的属性
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.
}

但是我想知道有没有更广泛和更动态的东西?某物将需要一对多的属性名称等价地图,以及代理类,并生成适配器?

解决方法

我从来没有使用过,但我认为你正在寻找 @L_301_1@:

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.

猜你在找的Java相关文章