其实不难理解,一看代码就知道spring用反射究竟多做了多少事情private static void copyProperties(Object source, Object target, Class<?> editable, 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) {
if (targetPd.getWriteMethod() != null &&(ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null && sourcePd.getReadMethod() != null) {
try {Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {readMethod.setAccessible(true);}Object value = readMethod.invoke(source);Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {writeMethod.setAccessible(true);}writeMethod.invoke(target, value);}
catch (Throwable ex) {
throw new FatalBeanException("Could not copy properties from source to target", ex);}}}}
}大致过程是,现根据object获得class,根据class获得PropertyDescriptor[],然后获得此property的WriteMethod,判断这个method是否是虚方法,是否是public,如果都ok了,就调用这个method赋值,【每一个method都会进行这轮判断】
详细解决方案
BeanUtils.copyProperties和普通赋值比较
热度:60 发布时间:2023-12-11 20:54:06.0
相关解决方案
- BeanUtils.copyProperties拷贝form到实业bean时出错
- [求助]关于BeanUtils.copyProperties(vo, po)的疑问
- BeanUtils copyproperties()集合属性可以复制吗?解决办法
- lang.ClassNotFoundException: org.apache.commons.beanutils.NestedNullException
- for exception javax.servlet.ServletException: BeanUtils.populate
- org.apache.commons.beanutils.ConversionException: Cannot
- BeanUtils.copyProperties(form,实体)后,form将值赋给html标签的原理 有关问题?
- BeanUtils.setProperty出错
- 如何解决Could not initialize class org.apache.commons.beanutils.BeanUtils
- BeanUtils.copyProperties 如何复制List
- ClassNotFoundException: org.apache.commons.beanutils.Converter解决方式
- 报错Duplicate classes in commons-collections and commons-beanutils
- 几个 BeanUtils 中的坑,千万别踩!
- BeanUtils.copyProperties(a, b);
- Beanutils.copyProperties( )用法及重写提高效率
- BeanUtils.populate(Object bean, Map properties) 详解
- commons-beanutils
- BeanUtils.copyProperties和普通赋值比较
- BeanUtils、BeanCopier、Dozer和Orika对比
- BeanUtils.copyProperties和clone()方法的区别
- Beanutils.copyproperties Javabean属性copy
- day41 JavaWeb阶段——综合案例:用户信息列表展示(Servlet+JSP+MySQL+JDBCTempleat+Duird+BeanUtilS+tomcat完成用户信息列表展示)
- BeanUtils.populate函数报错
- BeanUtils.populate方法
- 【转存学习】BeanUtils 是用 Spring 的还是 Apache 的好?
- 基于 asm 实现比 spring BeanUtils.copyProperty 性能更好的属性拷贝框架
- BeanUtils.copyProperties(ebOrder,ebShipAddr);
- org.springframework.beans.BeanUtils.copyProperties用法
- 两难!到底用 Spring BeanUtils 还是 Apache BeanUtils?
- BeanUtils.copyProperties(a, b); 用法记录