[alibaba/easyexcel]Debug了许久,总算把问题找全了

2024-05-11 915 views
0

使用easyExcel遇到一个bug,debug了好久 总算把问题找到了,分享出来。 问题复现重启服务时,由于org.springframework.cglib.beans.BeanMap 和 net.sf.cglib.beans.BeanMap 冲突,第一次使用easyExcel导出会报出以下错误,然后第二次之后就好了。 net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:237) ~[cglib-3.1.jar!/:na] at net.sf.cglib.beans.BeanMap$Generator.create(BeanMap.java:122) ~[cglib-3.1.jar!/:na] at net.sf.cglib.beans.BeanMap.create(BeanMap.java:58) ~[cglib-3.1.jar!/:na] at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addJavaObjectToExcel(ExcelWriteAddExecutor.java:119) ~[easyexcel-2.2.6.jar!/:na] at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addOneRowOfDataToExcel(ExcelWriteAddExecutor.java:72) ~[easyexcel-2.2.6.jar!/:na] at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.add(ExcelWriteAddExecutor.java:56) ~[easyexcel-2.2.6.jar!/:na] at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:58) ~[easyexcel-2.2.6.jar!/:na] at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:161) ~[easyexcel-2.2.6.jar!/:na] at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:146) ~[easyexcel-2.2.6.jar!/:na] at com.alibaba.excel.write.builder.ExcelWriterSheetBuilder.doWrite(ExcelWriterSheetBuilder.java:61) ~[easyexcel-2.2.6.jar!/:na] at gms.common.boot.CommonImportComponent.invoke(CommonImportComponent.java:94) ~[classes!/:0.0.1-SNAPSHOT] at gms.common.boot.CommonImportComponent.matrixCellsTmpReportData(CommonImportComponent.java:64) ~[classes!/:0.0.1-SNAPSHOT] at gms.mdm.biz.domain.service.impl.WorkCenterAssignEmployeeImportServiceImpl.processTmpData(WorkCenterAssignEmployeeImportServiceImpl.java:124) ~[classes!/:0.0.1-SNAPSHOT] at gms.mdm.biz.domain.service.impl.WorkCenterAssignEmployeeImportServiceImpl$$FastClassBySpringCGLIB$$7c71464e.invoke(<generated>) [classes!/:0.0.1-SNAPSHOT] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) [spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) [spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) [spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at gms.mdm.biz.domain.service.impl.WorkCenterAssignEmployeeImportServiceImpl$$EnhancerBySpringCGLIB$$4832bb50.processTmpData(<generated>) ~[classes!/:0.0.1-SNAPSHOT] at gms.mdm.biz.domain.service.impl.WorkCenterAssignEmployeeImportServiceImpl.processData(WorkCenterAssignEmployeeImportServiceImpl.java:84) ~[classes!/:0.0.1-SNAPSHOT] at gms.mdm.biz.domain.service.impl.WorkCenterAssignEmployeeImportServiceImpl$$FastClassBySpringCGLIB$$7c71464e.invoke(<generated>) [classes!/:0.0.1-SNAPSHOT] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) [spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) [spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) [spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at gms.mdm.biz.domain.service.impl.WorkCenterAssignEmployeeImportServiceImpl$$EnhancerBySpringCGLIB$$4832bb50.processData(<generated>) ~[classes!/:0.0.1-SNAPSHOT] at gms.mdm.biz.domain.job.WorkCenterAssignEmployeeImportJob.execute(WorkCenterAssignEmployeeImportJob.java:34) ~[classes!/:0.0.1-SNAPSHOT] at org.hzero.boot.scheduler.app.service.impl.JobExecuteServiceImpl.jobRun(JobExecuteServiceImpl.java:128) ~[hzero-boot-scheduler-1.3.0.RELEASE.jar!/:1.3.0.RELEASE] at org.hzero.boot.scheduler.app.service.impl.JobExecuteServiceImpl.jobExecute(JobExecuteServiceImpl.java:99) ~[hzero-boot-scheduler-1.3.0.RELEASE.jar!/:1.3.0.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_265] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_265] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_265] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_265] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197) [spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) [spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115) ~[spring-aop-5.0.10.RELEASE.jar!/:5.0.10.RELEASE] at org.apache.skywalking.apm.plugin.spring.async.SWCallable.call(SWCallable.java:47) ~[na:na] at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_265] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_265] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_265] at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_265] Caused by: java.lang.reflect.InvocationTargetException: null at sun.reflect.GeneratedMethodAccessor32.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_265] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_265] at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:384) ~[cglib-3.1.jar!/:na] at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:219) ~[cglib-3.1.jar!/:na] ... 47 common frames omitted Caused by: java.lang.LinkageError: loader (instance of org/springframework/boot/loader/LaunchedURLClassLoader): attempted duplicate class definition for name: "gms/mdm/biz/domain/vo/WorkCenterAssignEmployeeVO$$BeanMapByCGLIB$$9d320210" at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_265] at java.lang.ClassLoader.defineClass(ClassLoader.java:756) ~[na:1.8.0_265] ... 52 common frames omitted 可以在spring框架中找到https://github.com/spring-projects/spring-framework/issues/25031 如果上面的没有解决你的问题。那么其实你也可以看看mybatis mybatis中用到了org.springframework.cglib.beans.BeanMap, 而easyExcel中用到了net.sf.cglib.beans.BeanMap。如果你先用EasyExcel然后再用Mybatis(@Param的入参对应的类和你BeanMap的类相同)(mybatis版本mybatis-3.4.2.jar)进行查询 那么mybatis就会报错。 以下报错的意思其实是同一个类被同一个类加载器加载了两遍 第二遍就会报错。 以下给你看看报错信息: org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:345) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) ~[na:1.8.0_211] at java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:1.8.0_211] at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.cglib.beans.BeanMap$Generator.create(BeanMap.java:127) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.cglib.beans.BeanMap.create(BeanMap.java:59) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.hzero.mybatis.parser.SqlParserInterceptor.getArgs(SqlParserInterceptor.java:113) ~[hzero-starter-mybatis-mapper-1.3.8.RELEASE.jar:1.3.8.RELEASE] at org.hzero.mybatis.parser.SqlParserInterceptor.intercept(SqlParserInterceptor.java:74) ~[hzero-starter-mybatis-mapper-1.3.8.RELEASE.jar:1.3.8.RELEASE] at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) [mybatis-3.4.2.jar:3.4.2] at com.sun.proxy.$Proxy479.query(Unknown Source) [na:na] at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) [mybatis-3.4.2.jar:3.4.2] at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141) [mybatis-3.4.2.jar:3.4.2] at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77) [mybatis-3.4.2.jar:3.4.2] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_211] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_211] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211] at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) [mybatis-spring-1.3.1.jar:1.3.1] at com.sun.proxy.$Proxy187.selectOne(Unknown Source) [na:na] at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166) [mybatis-spring-1.3.1.jar:1.3.1] at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82) [mybatis-3.4.2.jar:3.4.2] at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) [mybatis-3.4.2.jar:3.4.2] at com.sun.proxy.$Proxy260.validateTeamLeader(Unknown Source) [na:na] at gms.common.util.Test.testMethod(Test.java:35) [test-classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_211] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_211] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211] at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) [junit-4.12.jar:4.12] at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12] at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) [junit-4.12.jar:4.12] at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.12.jar:4.12] at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73) [spring-test-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83) [spring-test-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) [spring-test-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) [spring-test-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) [spring-test-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) [junit-4.12.jar:4.12] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251) [spring-test-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12] at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.12.jar:4.12] at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) [junit-rt.jar:na] at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) [junit-rt.jar:na] at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) [junit-rt.jar:na] at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58) [junit-rt.jar:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_211] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_211] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211] at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:64) [idea_rt.jar:na] Caused by: java.lang.reflect.InvocationTargetException: null at sun.reflect.GeneratedMethodAccessor58.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_211] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211] at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:459) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:336) ~[spring-core-5.0.10.RELEASE.jar:5.0.10.RELEASE] ... 64 common frames omitted Caused by: java.lang.LinkageError: loader (instance of java/net/URLClassLoader): attempted duplicate class definition for name: "gms/mdm/biz/domain/vo/WorkCenterAssignEmployeeVO$$BeanMapByCGLIB$$9d320210" at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_211] at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[na:1.8.0_211] ... 69 common frames omitted 目前的解决方法是当easyExcel和mybatis一起用的时候,得注意下mybatis中的入参类不要和easyExcel相同

回答

1

不知道后续版本 easyExcel能否改以下BeanMap依赖,最好能够和mabatis一致把?

2

同时使用 spring & cglib 的 BeanMap导致 ,后续版本会修复,easyexcel会自己创建一个命名策略。

0

已经在3.0.0-beta1 版本修复,beta版本会在一个月内升级成正式版。