java BeanUtils.copyProperties

BeanUtils.copyProperties

  • 最近在使用BeanUtils.copyProperties类来进行 java对象类型赋值转化的时候发现有一些网上的博客,有些错误 有些把内容都弄反了

作用

  • 进行对象的转化
  • BeanUtils.copyProperties(“要转化的对象”," 转化的数据对象")

                val temp = new com.dailuobo.devops.thor.data.Machine();
                BeanUtils.copyProperties(machine, temp);  
上面是把machine的所有值copy给temp对象

如果我们获取的是macine中的数据内容,但是想要传递的是temp这个对象的数据结构就可以使用上面的,BeanUtils.copyProperties(),
主要是解决我们的对象的类型的转化

源码 说的很清楚

/**
	 * Copy the property values of the given source bean into the target bean.
	 * <p>Note: The source and target classes do not have to match or even be derived
	 * from each other, as long as the properties match. Any bean properties that the
	 * source bean exposes but the target bean does not will silently be ignored.
	 * <p>This is just a convenience method. For more complex transfer needs,
	 * consider using a full BeanWrapper.
	 * @param source the source bean
	 * @param target the target bean
	 * @throws BeansException if the copying failed
	 * @see BeanWrapper
	 */
	public static void copyProperties(Object source, Object target) throws BeansException {
		copyProperties(source, target, null, (String[]) null);
	}

把source中的值copy给target

所以有些事看源码就能全部解决的 ,大家要多看源码来解决问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lovely_red_scarf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值