BeanUtils.copyProperties的用法


BeanUtils它提供了对java反射和自省API的包装。它里面还有很多工具类,这里介绍一下copyProperties这个类,在开发中运用蛮多。,? 我们如果有两个具有很多相同属性的JavaBean,一个很常见的情况就是Struts里的PO对象(持久对象)和对应的ActionForm,传统的方式对属性逐个赋值就是采用原始的查询并赋值的方式,具体情况如下:,这种赋值方式其实很麻烦,那下面的话,就可以使用BeanUtils.copyProperties来进行快速赋值。,下面进行介绍...,示例:,使用规则如下:,BeanUtils.copyProperties(x, y);,下面附上完整示例代码,方便大家进行理解:,简介

BeanUtils它提供了对java反射和自省API的包装。它里面还有很多工具类,这里介绍一下copyProperties这个类,在开发中运用蛮多。


使用情况

? 我们如果有两个具有很多相同属性的JavaBean,一个很常见的情况就是Struts里的PO对象(持久对象)和对应的ActionForm,传统的方式对属性逐个赋值就是采用原始的查询并赋值的方式,具体情况如下:

casesUserIntegralEntity.setPluginId(casesUserIntegralEntityList.get(i).getPluginId());
	casesUserIntegralEntity.setTypeKey(casesUserIntegralEntityList.get(i).getTypeKey());
casesUserIntegralEntity.setPrimaryId(casesUserIntegralEntityList.get(i).getPrimaryId());

casesUserIntegralEntity.setName(casesUserIntegralEntityList.get(i).getName());
  casesUserIntegralEntity.setIntegralUser(casesUserIntegralEntityList.get(i).getIntegralUser());   
                  casesUserIntegralEntity.setIntegralStanderd(casesUserIntegralEntityList.get(i).getIntegralStanderd());

这种赋值方式其实很麻烦,那下面的话,就可以使用BeanUtils.copyProperties来进行快速赋值。

下面进行介绍...


BeanUtils.copyProperties()用法 格式
BeanUtils.copyProperties("转换前的类", "转换后的类");  

示例:

 BeanUtils.copyProperties(casesUserIntegralEntity,casesUserIntegral);

使用规则如下:

BeanUtils.copyProperties(x, y);

  1. y中的存在的属性,x中一定要有,但是x中可以有多余的属性;
  2. x中与y中相同的属性都会被替换,不管是否有值;
  3. x、 y中的属性要名字相同,才能被赋值,不然的话需要手动赋值;
  4. Spring的BeanUtils的CopyProperties方法需要对应的属性有getter和setter方法;
  5. 如果存在属性完全相同的内部类,但是不是同一个内部类,即分别属于各自的内部类,spring会认为属性不同,不会copy;
  6. spring和apache的copy属性的方法源和目的参数的位置正好相反,所以导包和调用的时候都要注意一下。

下面附上完整示例代码,方便大家进行理解:

	private static void copyProperties(Object source, Object target, @Nullable Class
  editable,
			@Nullable String... ignoreProperties) throws BeansException {

		Assert.notNull(source, "Source must not be null");
		Assert.notNull(target, "Target must not be null");

		Class
  actualEditable = target.getClass();
		if (editable != null) {
			if (!editable.isInstance(target)) {
				throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
						"] not assignable to Editable class [" + editable.getName() + "]");
			}
			actualEditable = editable;
		}
		PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
		List
 
   ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);

		for (PropertyDescriptor targetPd : targetPds) {
			Method writeMethod = targetPd.getWriteMethod();
			if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
				PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
				if (sourcePd != null) {
					Method readMethod = sourcePd.getReadMethod();
					if (readMethod != null &&
							ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
						try {
							if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
								readMethod.setAccessible(true);
							}
							Object value = readMethod.invoke(source);
							if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
								writeMethod.setAccessible(true);
							}
							writeMethod.invoke(target, value);
						}
						catch (Throwable ex) {
							throw new FatalBeanException(
									"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
						}
					}
				}
			}
		}
	}

 

,BeanUtils它提供了对java反射和自省API的包装。它里面还有很多工具类,这里介绍一下copyProperties这个类,在开发中运用蛮多。

上一篇:常用Lambda表达式

下一篇:阿里巴巴项目P8技术咖总结的Java心得,完整版PDF可下载


蚂蚁钢琴网 2008-2025 somall.com.cn 皖ICP备2023010105号
大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器
钢琴调律 钢琴调音 钢琴调律价格
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!
违法和不良信息24小时举报热线:18056540210