BeanUtils { public static void copyProperties(Object src, Object dst, String... ignoreFields) { Class srcClazz = src.getClass(); Class distClazz = dst.getClass(); Method[] methods = distClazz.getMethods(); List<String> ignoreFiledList = Arrays.asList(ignoreFields); for (Method dstMethod : methods) { if (Modifier.isStatic(dstMethod.getModifiers()) || !ReflectUtils.isBeanPropertyReadMethod(dstMethod)) { continue; } String propertyName = ReflectUtils.getPropertyNameFromBeanReadMethod(dstMethod); if (ignoreFiledList.contains(propertyName)) { continue; } Class dstReturnType = dstMethod.getReturnType(); try { Method dstSetterMethod = ReflectUtils.getPropertySetterMethod(distClazz, propertyName, dstReturnType); if (dstSetterMethod != null) { Method srcGetterMethod = ReflectUtils.getPropertyGetterMethod(srcClazz, propertyName); Class srcReturnType = srcGetterMethod.getReturnType(); if (srcReturnType.equals(dstReturnType)) { Object val = srcGetterMethod.invoke(src); if (val != null) { dstSetterMethod.invoke(dst, val); } } } } catch (Exception ignore) { } } }  static void setProperty(Object bean, String name, Class<T> clazz, T value); static T getProperty(Object bean, String name, Class<T> clazz); static void copyPropertiesToMap(Object bean, String prefix, Map<String, Object> map); static void copyProperties(Object src, Object dst, String... ignoreFields); static List<String> getModifiedFields(T src, T dst, String... ignoreFields);  }
@Test public void testCopyPropterties() throws Exception { TestBean bean = new TestBean(); bean.setAlias("aaa:1.0.0"); List<TestSubBean> subBeans = new ArrayList<TestSubBean>(); TestSubBean sub = new TestSubBean(); sub.setName("xxxxxx"); sub.setParameter("maaaaak", "maaaav"); subBeans.add(sub); bean.setSubBeans(subBeans); TestOtherBean otherBean = new TestOtherBean(); BeanUtils.copyProperties(bean, otherBean, "alias"); Assert.assertEquals(bean.getHeartbeat(), otherBean.getHeartbeat()); Assert.assertFalse(bean.getAlias().equals(otherBean.getAlias())); Assert.assertEquals(bean.getSubBeans(), otherBean.getSubBeans()); Assert.assertEquals(bean.isRegister(), otherBean.isRegister()); }