Spring巴斯ExcelPoEtemReader

Spring Batch Excel PoiItemReader

我正在尝试使用带有 PoiItemReader 的 spring-批处理扩展(spring-批处理-excel)来读取 excel 罚款。尝试通过以下配置读取 excel 文件时出现以下错误。

错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.xlsItemReader' defined in class path resource [com/timestreamgroup/reconstream/kafkabatchproducer/batch/config/xls/XlsBatchConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.batch.extensions.excel.poi.PoiItemReader] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@3b192d32]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean(AbstractBeanFactory.java:374)
    at org.springframework.batch.core.scope.StepScope.get(StepScope.java:113)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:371)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:676)
    at org.springframework.batch.extensions.excel.poi.PoiItemReader$$EnhancerBySpringCGLIB$fab7ddf.open(<generated>)
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:104)
    at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:311)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:205)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:152)
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:413)
    at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:136)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:320)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:149)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:140)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:128)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
    at com.sun.proxy.$Proxy147.run(Unknown Source)
    at com.timestreamgroup.reconstream.kafkabatchproducer.filedir.config.FileToJobTransformer.lambda$adapt(FileToJobTransformer.java:201)
    at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run$$$capture(CompletableFuture.java:1736)
    at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java)
    at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1728)
    at java.base/java.util.concurrent.ForkJoinTask.doExec$$$capture(ForkJoinTask.java:290)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
    at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.batch.extensions.excel.poi.PoiItemReader] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@3b192d32]
    at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:739)
    at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:671)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.buildPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:407)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:388)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1098)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
    ... 39 common frames omitted
Caused by: java.lang.NoClassDefFoundError: Lorg/apache/poi/ss/usermodel/Workbook;
    at java.base/java.lang.Class.getDeclaredFields0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredFields(Class.java:3062)
    at java.base/java.lang.Class.getDeclaredFields(Class.java:2249)
    at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:734)
    ... 45 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 49 common frames omitted

2022-01-04 19:24:42,665 batch-producer-service INFO [ForkJoinPool.commonPool-worker-3] AbstractStep: Step: [xlsStep1] executed in 2s592ms
2022-01-04 19:24:44,151 batch-producer-service ERROR [ForkJoinPool.commonPool-worker-3] AbstractStep: Exception while closing step execution resources in step xlsStep1 in job xlsJob

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.xlsItemReader' defined in class path resource [com/timestreamgroup/reconstream/kafkabatchproducer/batch/config/xls/XlsBatchConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.batch.extensions.excel.poi.PoiItemReader] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@3b192d32]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean(AbstractBeanFactory.java:374)
    at org.springframework.batch.core.scope.StepScope.get(StepScope.java:113)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:371)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:676)
    at org.springframework.batch.extensions.excel.poi.PoiItemReader$$EnhancerBySpringCGLIB$fab7ddf.close(<generated>)
    at org.springframework.batch.item.support.CompositeItemStream.close(CompositeItemStream.java:90)
    at org.springframework.batch.core.step.tasklet.TaskletStep.close(TaskletStep.java:306)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:287)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:152)
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:413)
    at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:136)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:320)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:149)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:140)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:128)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
    at com.sun.proxy.$Proxy147.run(Unknown Source)
    at com.timestreamgroup.reconstream.kafkabatchproducer.filedir.config.FileToJobTransformer.lambda$adapt(FileToJobTransformer.java:201)
    at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run$$$capture(CompletableFuture.java:1736)
    at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java)
    at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1728)
    at java.base/java.util.concurrent.ForkJoinTask.doExec$$$capture(ForkJoinTask.java:290)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
    at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.batch.extensions.excel.poi.PoiItemReader] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@3b192d32]
    at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:739)
    at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:671)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.buildPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:407)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:388)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1098)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
    ... 39 common frames omitted
Caused by: java.lang.NoClassDefFoundError: Lorg/apache/poi/ss/usermodel/Workbook;
    at java.base/java.lang.Class.getDeclaredFields0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredFields(Class.java:3062)
    at java.base/java.lang.Class.getDeclaredFields(Class.java:2249)
    at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:734)
    ... 45 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 49 common frames omitted

ItemReader 配置:

@Bean
    @Scope(value = "step", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public PoiItemReader xlsItemReader() {
        PoiItemReader reader = new PoiItemReader();
        reader.setResource(new FileSystemResource("/path/to/your/excel/file"));
        reader.setRowMapper(new PassThroughRowMapper());
        return reader;
    }

这里是 pom 依赖项

<dependency>
            <groupId>org.springframework.batch.extensions</groupId>
            <artifactId>spring-batch-excel</artifactId>
            <version>0.1.0</version>
        </dependency>

示例 Excel 我正在尝试读取的文件

看来您需要自己为 poi jar 添加显式依赖项 - 它们在 https://github.com/spring-projects/spring-batch-extensions/blob/main/spring-batch-excel/pom.xml

中标记为可选