加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

规范化Java bean属性名称

发布时间:2020-12-14 05:28:48 所属栏目:Java 来源:网络整理
导读:我有一堆第三方 Java类,它们使用不同的属性名称来实现相同的属性: public class Foo { public String getReferenceID(); public void setReferenceID(String id); public String getFilename(); public void setFilename(String fileName);}public class Ba
我有一堆第三方 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.
}

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

解决方法

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

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读