兩個實體類之間進行互換


 


 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());

  


 如果按照上面那種賦值方式,是非常麻煩的,而且代碼量可以估計會很多


 這時候我們如果用copyProperties,直接一行代碼,然后就搞定了。

BeanUtils.copyProperties("轉換前的類", "轉換后的類");

例如:

BeanUtils.copyProperties(casesUserIntegralEntity,casesUserIntegral);

  

但是我們需要注意:

BeanUtils.copyProperties(a, b);

b中的存在的屬性,a中一定要有,但是a中可以有多余的屬性;
a中與b中相同的屬性都會被替換,不管是否有值;
a、 b中的屬性要名字相同,才能被賦值,不然的話需要手動賦值;
Spring的BeanUtils的CopyProperties方法需要對應的屬性有getter和setter方法;
如果存在屬性完全相同的內部類,但是不是同一個內部類,即分別屬於各自的內部類,則spring會認為屬性不同,不會copy;
spring和apache的copy屬性的方法源和目的參數的位置正好相反,所以導包和調用的時候都要注意一下。
源碼
 最后附上源碼,方便大家可以更好地理解他的屬性;

/**
* Copy the property values of the given source bean into the given 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.
* @param source the source bean
* @param target the target bean
* @param editable the class (or interface) to restrict property setting to
* @param ignoreProperties array of property names to ignore
* @throws BeansException if the copying failed
* @see BeanWrapper
*/
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<String> 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);
}
}
}
}
}
}

  另外,apache和springboot下面都有這個工具類,順序是顛倒的,引包需要注意。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM