带 StepScope Annotation 的 PoiItemReader 不读取 Excel 文件

2024-03-12

我想将 Spring Batch 中的 JobParameter 传递给我的 PoiItemReader,以找到 Excelfilepath。所以我必须使用注释@StepScope

@StepScope
@Bean
ItemReader<StudentDTO> excelStudentReader( @Value("#{jobParameters[filePath]}") String filePath) {
    PoiItemReader<StudentDTO> reader = new PoiItemReader<>();
    reader.setResource(new ClassPathResource(filePath));
    reader.setRowMapper(new StudentExcelRowMapper());
    return reader;
}

作业启动无异常,但读取器不读取 Excelfile。

Job: [FlowJob: [name=importUserJob]] launched with the following parameters: [{filePath=files/sample-data.xlsx, time=1515056077325}]  
Executing step: [step1]  
Job: [FlowJob: [name=importUserJob]] completed with the following parameters: [{filePath=files/sample-data.xlsx, time=1515056077325}] and the following status: [COMPLETED]  

如果我删除注释@StepScope并将路径直接提供给ItemReader,PoiItemReader读取ExcelFile。

Job: [FlowJob: [name=importUserJob]] launched with the following parameters: [{filePath=files/sample-data.xlsx, time=1515055832722}]  
Executing step: [step1]
StudentDTO [[email protected] /cdn-cgi/l/email-protection , name=Tony Tester, purchasedPackage=master]  
StudentDTO [[email protected] /cdn-cgi/l/email-protection, name=Nick Newbie , purchasedPackage=starter]  
StudentDTO [[email protected] /cdn-cgi/l/email-protection, name=Ian Intermediate, purchasedPackage=intermediate]  
Job: [FlowJob: [name=importUserJob]] completed with the following parameters: [{filePath=files/sample-data.xlsx, time=1515055966700}] and the following status: [COMPLETED]

我有 PoiItemReader 来自https://github.com/mdeinum/spring-batch-extensions https://github.com/mdeinum/spring-batch-extensions
代码来自教程:https://www.petrikainulainen.net/programming/spring-framework/spring-batch-tutorial-reading-information-from-an-excel-file/ https://www.petrikainulainen.net/programming/spring-framework/spring-batch-tutorial-reading-information-from-an-excel-file/

批量配置.java

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

  @Autowired
  public JobBuilderFactory jobBuilderFactory;

  @Autowired
  public StepBuilderFactory stepBuilderFactory;

  @StepScope
  @Bean
  ItemReader<StudentDTO> excelStudentReader( @Value("#
{jobParameters[filePath]}") String filePath) {
      PoiItemReader<StudentDTO> reader = new PoiItemReader<>();
      reader.setResource(new ClassPathResource(filePath));
      reader.setLinesToSkip(1);
      reader.setRowMapper(new StudentExcelRowMapper());
      return reader;
  }

    public CustomWriter writer() {
      return new CustomWriter();
    }

  @Bean
  public Job importUserJob() {
      return jobBuilderFactory.get("importUserJob")
              .incrementer(new RunIdIncrementer())
              .flow(step1())
              .end()
              .build();
  }

  @Bean
  public Step step1() {
      return stepBuilderFactory.get("step1")
              .<StudentDTO, StudentDTO> chunk(10)
              .reader(excelStudentReader(null))
              .writer(writer())
              .build();
  }
}

CustomWriter.java

public class CustomWriter implements ItemWriter<StudentDTO> {

  public void write(List<? extends StudentDTO> arg0) throws Exception {
      for (StudentDTO s : arg0){
          System.out.println(s.toString());
      }
  }

}

应用程序.java

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
public class Application {

public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
    try {
        JobParameters param = new JobParametersBuilder().addString("filePath", "files/sample-data.xlsx")
                .addLong("time", System.currentTimeMillis()).toJobParameters();
         ApplicationContext context = new AnnotationConfigApplicationContext(BatchConfiguration.class);
        JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean(Job.class);
        jobLauncher.run(job, param);
      } catch (Exception e) {
        System.out.println(e.getMessage());
      }

  }
}

设置 StepScope 注释对于访问 JobParameters 是必需的,并且将参数传递给 FlatFileItemReader 没有任何问题。我认为问题出在 PoiItemReader 上。

如何使用 PoiItemReader 从 Spring Batch 中的 JobParameters 获取 filePath 来读取 excelFile?


@Step范围不适用于通用阅读器,即ItemReader<StudentDTO>你需要将其更改为PoiItemReader<StudentDTO>

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带 StepScope Annotation 的 PoiItemReader 不读取 Excel 文件 的相关文章

  • JUnit 使用 Mockito 测试异步方法

    我已经使用 Spring Framework 版本 5 0 5 RELEASE 在 Java 1 8 类中实现了异步方法 public class ClassToBeTested Autowired private MyComponent
  • 如何在Spring的applicationContext.xml中指定默认范围来请求范围?

    我想让所有 bean 请求默认作用域 但是 Spring 文档说默认作用域是 Singleton 第 3 4 1 和 3 4 2 节http static springsource org spring docs 2 5 x referen
  • 使用Spring Cloud Stream Kafka动态更改instanceindex

    如同 在运行时更改 spring cloud stream 实例索引 计数 https stackoverflow com questions 37579939 changing spring cloud stream instance i
  • 如何在具有动态列的表中插入值 Jdbc/Mysql

    我想在具有动态列的表中添加值 我设法创建一个包含动态列的表 但我不知道如何插入数据 Create Table sql CREATE TABLE MyDB myTable level INTEGER 255 int columnNumber
  • 无法从后台服务通过 WiFi 访问互联网

    我将直接介绍我发现的一些事实 数据 如果您遇到 解决了类似的问题 请帮助我 我每 5 分钟向服务器发送一次数据 除非用户在服务器的帮助下手动将其关闭 wakeful broadcast receiver通过一个intent service
  • 业务代表与服务定位器

    Business Delegate 和 Service Locator 之间有什么区别 两者都负责封装查找和创建机制 如果 Business Delegate 使用 Service Locator 来隐藏查找和创建机制 那么 Busines
  • Maven 多模块项目结构问题

    自从过去几周构建我的 Maven 多模块项目以来 这是我的一次有趣的经历 当我决定使用 Maven 进行构建生命周期管理时 我有几个原因希望选择 Maven A 大多数开发团队都是分开的 这样每个团队都可以在项目中的单独模块上工作 例如团队
  • 会话 bean 中的 EntityManager 异常处理

    我有一个托管无状态会话 bean 其中注入了 EntityManager em 我想做的是拥有一个具有唯一列的数据库表 然后我运行一些尝试插入实体的算法 但是 如果实体存在 它将更新它或跳过它 我想要这样的东西 try em persist
  • 如何使用 Java 原生接口从 Java 调用 Go 函数?

    可以通过以下方式调用 C 方法JNA https en wikipedia org wiki Java Native AccessJava 中的接口 如何使用 Go 实现相同的功能 package main import fmt impor
  • grails 上的同步块在 Windows 上有效,但在 Linux 上无效

    我有一个 grails 应用程序 它依赖于服务中的同步块 当我在 Windows 上运行它时 同步按预期工作 但当我在 ams linux 上运行时 会出现 StaleObjectStateException 该问题在以下示例中重现 cla
  • 当容器大小更改时,JTable 仅调整选定列的大小

    对于面板内的 JTable 如果面板变大 我如何将额外的空间仅分配给某些列 在我的例子中 分配给最后一列 尽管提供 第 3 4 列和8 将获得额外的空间 我想允许用户手动更改所有列的列大小 我尝试了 table setAutoResizeM
  • 将字符串中的字符向左移动

    我是 Stack Overflow 的新手 有一道编程课的实验室问题一直困扰着我 该问题要求我们将字符串 s 的元素向左移动 k 次 例如 如果输入是 Hello World 和3 它将输出 lo WorldHel 对于非常大的 k 值 它
  • Scala repl 抛出错误

    当我打字时scala在终端上启动 repl 它会抛出此错误 scala gt init error error while loading AnnotatedElement class file usr lib jvm java 8 ora
  • Android同步onSensorChanged?

    这是我的问题的后续 Android线程可运行性能 https stackoverflow com questions 36395440 android thread runnable performance 我在理解应用程序的同步方法时遇到
  • 用于将 MS Word 表导出到 Excel 工作表的宏

    我有一个包含许多表格的word文档 有谁知道如何编写宏将此类表导出到不同的 Excel 工作表 答案摘自 http www mrexcel com forum showthread php t 36875 http www mrexcel
  • 为什么spring boot 1.5.3 jar无法识别src/main/resources/META-INF/resources/中的jsp文件

    我使用了spring boot jsp 我想构建一个可执行的jar 如下这个帖子 http www logicbig com tutorials spring framework spring boot boot serve dynamic
  • 日期时间解析异常

    解析日期时 我的代码中不断出现异常错误 日期看起来像这样 Wed May 21 00 00 00 EDT 2008 这是尝试读取它的代码 DateTimeFormatter formatter DateTimeFormatter ofPat
  • 如何在Java中跨类共享变量,我尝试了静态不起作用

    类 Testclass1 有一个变量 有一些执行会改变变量的值 现在在同一个包中有类 Testclass2 我将如何访问 Testclass2 中变量的更新值 由 Testclass1 更新 试过这个没用 注意 Testclass1和Tes
  • 将隐藏(生物识别)数据附加到 pdf 上的数字签名

    我想知道是否可以使用 iText 我用于签名 或 Java 中的其他工具在 pdf 上添加生物识别数据 我会更好地解释一下 在手写板上签名时 我会收集签名信息 例如笔压 签名速度等 我想将这些信息 java中的变量 与pdf上的签名一起存储
  • 如何使用 Spring AOP 建议静态方法?

    在执行类的静态方法之前和之后需要完成一些日志记录 我尝试使用 Spring AOP 来实现这一点 但它不起作用 而对于正常方法来说它起作用 请帮助我理解如何实现这一点 如果可以使用注释来完成 那就太好了 也许您应该在使用 Spring AO

随机推荐