ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

BeanUtils.copyProperties(source, target)报NullPointException

2021-09-03 02:31:06  阅读:259  来源: 互联网

标签:CachedIntrospectionResults java BeanUtils springframework source copyProperties 


遇到一件怪事,今天在测试一段代码时老时报NullPointException

代码:

 

---  ObjectUtil.java  
    
        JdOrder jdOrder = new JdOrder();
        jdOrder.setSkuId(132323324l);
        jdOrder.setCommissionRate(BigDecimal.valueOf(0.53));
        jdOrder.setEstimateCosPrice(BigDecimal.valueOf(0.43));
        ShopguideOrder order = new ShopguideOrder();

        BeanUtils.copyProperties(jdOrder,order);

 

Exception in thread "main" java.lang.NullPointerException
    at org.springframework.core.BridgeMethodResolver.findBridgedMethod(BridgeMethodResolver.java:69)
    at org.springframework.beans.GenericTypeAwarePropertyDescriptor.<init>(GenericTypeAwarePropertyDescriptor.java:71)
    at org.springframework.beans.CachedIntrospectionResults.buildGenericTypeAwarePropertyDescriptor(CachedIntrospectionResults.java:353)
    at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:307)
    at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:188)
    at org.springframework.beans.BeanUtils.getPropertyDescriptors(BeanUtils.java:360)
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:608)
    at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:543)
    at com.davidhu.shopguide.api.utils.ObjectUtil.main(ObjectUtil.java:128)

看源代码看了不少时间,只时知道类里有一个PropertyDescriptor propertyName = class,然后这个属性没有writemethod,然后就报错。但这个方法是copy对象属性的常用方法,不应该有问题呀。然后我把代码拷贝到

ThirdApiTest里又跑了一下,奇怪,在这里又没问题了

google了一下,果然还是找到了

https://github.com/spring-projects/spring-framework/issues/24177

Issue:
Failure in BeanUtils.copyProperties(source, target)

Stack Trace:
Exception in thread "main" java.lang.NullPointerException at org.springframework.core.BridgeMethodResolver.findBridgedMethod(BridgeMethodResolver.java:69) at org.springframework.beans.GenericTypeAwarePropertyDescriptor.(GenericTypeAwarePropertyDescriptor.java:70) at org.springframework.beans.CachedIntrospectionResults.buildGenericTypeAwarePropertyDescriptor(CachedIntrospectionResults.java:359) at org.springframework.beans.CachedIntrospectionResults.(CachedIntrospectionResults.java:316) at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:186) at org.springframework.beans.BeanUtils.getPropertyDescriptors(BeanUtils.java:360) at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:608) at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:579)

Initial Analysis:

findBridgedMethod(Method bridgeMethod) method of BridgeMethodResolver was refactored as part of Spring 5.0 release.
Whenever this method is invoked for getting writeMethodToUse for propertyName = class, it results in Null Pointer exception as isBridge() method of bridgeMethod is invoked which is null.

回答:

 
Contributor
jhoeller commented on Dec 10, 2019
It seems that you're running spring-beans-4.3.x against spring-core-5.x.x there. We don't support that, the Spring Framework module versions need to be in sync. Please make sure that your classpath is consistent in that respect.

spring-beans-5.x.x has corresponding non-null guards at that position so bypasses the BridgeMethodResolver call upfront.

焕然大悟,原来在ObjectUtil.java用到的spring-beans-4.0.9,而运行在ThirdApiTest 里跑的是spring-beans-5.3.1,因为ObjectUtil.java所在工程是api,而ThirdApiTest所在工程是admin。

那为什么api用的是spring-beans-4.0.9?在api下面有个idea生成的文件api.iml,上面有

    <orderEntry type="library" name="Maven: org.springframework:spring-beans:4.0.9.RELEASE" level="project" />
删掉重新生成,还是依赖了4.0.9包。分析了api的pom.xml,发现这个spring是依赖于swagger的,因为api没有单独引入spring的任何jar包。到此,真相大白了。



 

标签:CachedIntrospectionResults,java,BeanUtils,springframework,source,copyProperties,
来源: https://www.cnblogs.com/zjhgx/p/15221495.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有