将一个对象的属性值赋给另一个对象的相同的属性 这两个对象必须都符合javaBean的标准

/***
* 将一个对象的属性值赋给另一个对象的相同的属性 这两个对象必须都符合javaBean的标准

* @param source
*            要赋值的源对象
* @param target
*            被赋值的目标对象
* @param ignoreProperties
*            被忽略赋值的属性数组
* @throws Exception

*/
@SuppressWarnings("unused")
public static void copyProperties(Object source, Object target, String ignoreProperties[]) throws Exception {
Class targetClass = target.getClass();
Class sourceClass = source.getClass();


// 得到目标对象和源对象的属性数组
PropertyDescriptor targetPds[] = Introspector.getBeanInfo(targetClass).getPropertyDescriptors();
PropertyDescriptor sourcetPds[] = Introspector.getBeanInfo(sourceClass).getPropertyDescriptors();
// 将忽略字段的数组转化为list
List<String> ignoreList = ignoreProperties == null ? null : Arrays.asList(ignoreProperties);
// 把源对象的所有属性放的map中
Map<String, PropertyDescriptor> sourcePropertyMap = new HashMap<String, PropertyDescriptor>();
for (int i = 0; i < sourcetPds.length; i++) {
PropertyDescriptor pd = sourcetPds[i];
sourcePropertyMap.put(pd.getName(), pd);
}
for (int i = 0; i < targetPds.length; i++) {
PropertyDescriptor targetPd = targetPds[i];


if (targetPd.getWriteMethod() == null || ignoreProperties != null && ignoreList.contains(targetPd.getName()))
continue;
PropertyDescriptor sourcePd = sourcePropertyMap.get(targetPd.getName());
if (sourcePd == null || sourcePd.getReadMethod() == null)
continue;
try {
// 得到源对象对应的属性值
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()))
readMethod.setAccessible(true);
Object value = readMethod.invoke(source, new Object[0]);
// 将源对象的属性值赋值给目标对象对应的属性
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()))
writeMethod.setAccessible(true);
writeMethod.invoke(target, new Object[] { value });
} catch (Throwable ex) {
throw new IllegalArgumentException("Could not copy properties from source to target", ex);
}
}


}
如果要将一个对象的全部属性赋值给另一个对象的部分属性,可以使用Java BeanUtils类或Apache Commons BeanUtils类库中的相关方法来实现。 Java BeanUtils类是Java标准库中的一个类,提供了对JavaBean属性的操作,包括读取、设置属性值等。使用BeanUtils类库,可以将一个对象属性值拷贝到另一个对象中,具体实现可以使用BeanUtils.copyProperties(Object dest, Object orig)方法。该方法会将orig对象中的属性值赋值到dest对象中,如果属性相同属性值会直接覆盖。需要注意的是,copyProperties()方法只会拷贝两个对象属性相同、类型相同属性值,如果两个对象中存在属性名不同或属性类型不同的属性,该方法会忽略它们。 如果要将一个对象的全部属性赋值给另一个对象的部分属性,可以先将原始对象属性值拷贝到一个新的对象中,然后再将新对象中的部分属性值赋值给目标对象。具体实现可以使用copyProperties()方法将原始对象属性值拷贝到一个对象中,然后使用BeanUtils.setProperty(Object bean, String name, Object value)方法将新对象中的部分属性值赋值给目标对象。其中,第一个参数为目标对象,第二个参数为属性名,第三个参数为属性值。 Apache Commons BeanUtils类库也提供了类似的方法,其中一个比较常用的是BeanUtils.copyProperties(Object dest, Object orig)方法,与Java BeanUtils类库中的方法类似,可以将orig对象中的属性值拷贝到dest对象中。需要注意的是,在Apache Commons BeanUtils类库中,可以使用PropertyUtils.getProperty(Object bean, String name)方法获取对象中指定属性的值,使用PropertyUtils.setProperty(Object bean, String name, Object value)方法设置对象中指定属性的值。 综上所述,使用Java BeanUtils类库或Apache Commons BeanUtils类库中的相关方法可以快速地将一个对象的全部属性赋值给另一个对象的部分属性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值