天天看點

BeanUtils,不同 jar 包所埋的坑

背景

  線上代碼,兩個地方使用了相同的copy 方式,但是執行的效果卻完全不一樣。

BeanUtils.copyProperties(invoiceMonth, invoiceMonthDTO);      

問題描述

1、我們的項目使用了 Spring 包和 Apache 包兩種,而這兩個包都提供了 BeanUtils 工具方法

2、看一下源碼:

   1)、Apache 源碼是,第一參數:dest;第二參數:source。

public static void copyProperties(Object dest, Object orig)

throws IllegalAccessException, InvocationTargetException {


BeanUtilsBean.getInstance().copyProperties(dest, orig);

}      

   2)、Spring 源碼是,第一參數:source;第二參數是 target。

public static void copyProperties(Object source, Object target) throws BeansException {

copyProperties(source, target, null, (String[]) null);

}      

綜上可以看出,兩個開源包提供的工具類是有差異的,一不小心就會因為 jar 包的錯誤引入導緻程式錯誤。

3、另外貼出集團規約:避免使用 apache BeanUtils

BeanUtils,不同 jar 包所埋的坑