用于合并java bean的工具 [英] Tools for merging java beans

查看:138
本文介绍了用于合并java bean的工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用内省合并两个简单的扁平java bean很容易:

It's easy to merge two simple, flat java beans using introspection:

    BeanInfo info = Introspector.getBeanInfo( ContactBean.class );
    PropertyDescriptor pDescArr[] = info.getPropertyDescriptors();
    for(PropertyDescriptor pDesc : pDescArr){
        //copy properties and check for conflicts here
    }

但是,当属性包含嵌套bean或集合时,它会变得更复杂一些。是否有一个智能工具可以处理复杂bean的深度合并?

However, it gets a little more complicated when the properties contain nested beans, or collections. Is there a smart tool somewhere which will handle a deep merge of complex beans?

关于我希望合并如何工作的更多细节:

Some more specifics on just how I'd like the merge to work:

给定源bean的集合和空目标bean,除非存在冲突,否则应将简单属性从源复制到目标。如果存在冲突,则该字段应为空。如果属性属于集合类型,则应合并源bean的值(不包括重复项),并将其复制到目标属性。这些规则应递归应用于bean本身的属性。

Given a collection of source beans, and an empty target bean, simple properties should be copied over from the source to the target, unless there's a conflict. If there's a conflict, the field should be left empty. If a property is of a collection type, the values of the source beans should be combined, excluding duplicates, and copied to the destination property. These rules should apply recursively to properties which are beans themselves.

推荐答案

Dozer Smooks 。如果您只想要bean合并,Dozer就是赢家。如果您正在寻找其他用例,如csv to pojo等,那么请看看Smooks。

Dozer or Smooks. Dozer is the winner if you just want bean merge. If you are looking for other usecases like csv to pojo etc then take a look at Smooks.

这篇关于用于合并java bean的工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆