栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

记录一次@Accessors(chain = true) 引发的BeanUtils的反射问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

记录一次@Accessors(chain = true) 引发的BeanUtils的反射问题

环境:JDK7,tomcat7 问题:bean对象使用了@Accessors(chain = true) 注解,

在调用BeanUtils.setProperty(t, field.getName(), objvalue);

由于链式调用重写了 set方法返回值为对象this,导致BeanUtils调用反射获取set方法时获取不到,最终没有对需要值的对象set进入值

但是实际测试和本地环境可以获取到值,而生产环境获取不到

网上找到拿不到值的原因

cglib中使用 Java rt.jar包中的java.beans.Introspector类,在java.beans.Introspector类520行:只获取返回值是void类型的setxxxx方法,而@Accessors(chain=true)下setter方法返回当前对象,这个显然会有问题。

                     if (argCount == 0) {
                         if (name.startsWith(GET_PREFIX)) {
                             // Simple getter
                             pd = new PropertyDescriptor(this.beanClass, name.substring(3), method, null);
                         } else if (resultType == boolean.class && name.startsWith(IS_PREFIX)) {
                             // Boolean getter
                             pd = new PropertyDescriptor(this.beanClass, name.substring(2), method, null);
                         }
                     } else if (argCount == 1) {
                         if (int.class.equals(argTypes[0]) && name.startsWith(GET_PREFIX)) {
                             pd = new IndexedPropertyDescriptor(this.beanClass, name.substring(3), null, null, method, null);
                             // 核心问题在这里如果set方法返回不是void则不把set方法放到当前类的
                             // private HashMap pdStore = new HashMap();中
                         } else if (void.class.equals(resultType) && name.startsWith(SET_PREFIX)) {
                             // Simple setter
                             pd = new PropertyDescriptor(this.beanClass, name.substring(3), null, method);
                             if (throwsException(method, PropertyVetoException.class)) {
                                 pd.setConstrained(true);
                             }
                         }
                     } else if (argCount == 2) {
                             if (void.class.equals(resultType) && int.class.equals(argTypes[0]) && name.startsWith(SET_PREFIX)) {
                             pd = new IndexedPropertyDescriptor(this.beanClass, name.substring(3), null, null, null, method);
                             if (throwsException(method, PropertyVetoException.class)) {
                                 pd.setConstrained(true);
                             }
                         }
                     }

这段代码结束后调用下一个方法processPropertyDescriptors

查看DefaultBeanIntrospector类的核心生产beanInfo

  public void introspect(final IntrospectionContext icontext) {
         BeanInfo beanInfo = null;
         try {
             // 这里通过上述类Introspector 获取bean,按照上面说法此时如果set方法返回的不是Null 依然还拿不到方法
             beanInfo = Introspector.getBeanInfo(icontext.getTargetClass());
         } catch (final IntrospectionException e) {
             // no descriptors are added to the context
             log.error(
                     "Error when inspecting class " + icontext.getTargetClass(),
                     e);
             return;
         }
 ​
         PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
         if (descriptors == null) {
             descriptors = new PropertyDescriptor[0];
         }
 ​
         handleIndexedPropertyDescriptors(icontext.getTargetClass(),
                 descriptors);
         icontext.addPropertyDescriptors(descriptors);
     }

整个方法走完此时icontext中添加的descriptors 对象属性值writeMethodName 依然是null

实际上apache除了默认提供的DefaultBeanIntrospector 使用jdk的内省Introspector

还提供了FluentPropertyBeanIntrospector 和SuppressPropertiesBeanIntrospector

其中FluentPropertyBeanIntrospector 可以解决上述问题

其方法introspect() ,在DefaultBeanIntrospector 执行后如果setWriteMethod还是null,会将只根据方法名称获取的方法装入

public void introspect(final IntrospectionContext icontext)
            throws IntrospectionException {
        for (final Method m : icontext.getTargetClass().getMethods()) {
            if (m.getName().startsWith(getWriteMethodPrefix())) {
                final String propertyName = propertyName(m);
                final PropertyDescriptor pd = icontext
                        .getPropertyDescriptor(propertyName);
                try {
                    if (pd == null) {
                        icontext.addPropertyDescriptor(createFluentPropertyDescritor(
                                m, propertyName));
                    } else if (pd.getWriteMethod() == null) {
                        pd.setWriteMethod(m);
                    }
                } catch (final IntrospectionException e) {
                    log.info("Error when creating PropertyDescriptor for " + m
                            + "! Ignoring this property.");
                    log.debug("Exception is:", e);
                }
            }
        }
    }

而SuppressPropertiesBeanIntrospector 是抑制内省 ,观察其introspect() 方法,

public void introspect(final IntrospectionContext icontext) throws IntrospectionException {
        for (final String property : getSuppressedProperties()) {
            icontext.removePropertyDescriptor(property);
        }
    }

会在实际过程中 过滤 构造SuppressPropertiesBeanIntrospector时 , 传入的final Collection propertiesToSuppress,集合进行方法过滤remove

当然也可以根据实际业务需求 继续BeanIntrospector 编写自定义的 内省过滤器

我们项目中后续使用了BeanConverter 直接 生成set get方法的实体类

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/776708.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号