You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3.2.x Batch从命令行传递JobParameter无法取值

Spring Boot 3.2.x中Spring Batch命令行JobParameters失效问题

问题描述

在Spring Boot 2.7.x中,通过两种方式传递JobParameters参数均能正常被StepScope的Tasklet获取:

  • 代码中通过JobParametersBuilder构建并传递参数
  • 启动时通过命令行传递参数

但迁移至Spring Boot 3.2.x后,命令行传递的参数无法被注入,仅代码构建的参数能正常获取。

相关代码

JobConfig.java

@Configuration
public class JobConfiguration {
    @Autowired
    private JobRepository jobRepository;

    @Autowired
    private PlatformTransactionManager manager;

    @Bean
    @StepScope
    public Tasklet helloWorldTasklet(@Value("#{jobParameters['message']}") String message,
                                     @Value("#{jobParameters['message1']}") String message1) {

        return (stepContribution, chunkContext) -> {
            System.out.println(message);
            System.out.println(message1);
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Step step1() {
        return new StepBuilder("step1", jobRepository)
                .tasklet(helloWorldTasklet(null, null), manager)
                .build();
    }

    @Bean
    public Job jobParametersJob() {
        return new JobBuilder("jobParametersJob", jobRepository)
                .start(step1())
                .build();
    }
}

JobParametersApplication.java

@SpringBootApplication
public class JobParametersApplication implements CommandLineRunner{
    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job job;
    
    public static void main(String[] args) {
        SpringApplication.run(JobParametersApplication.class, "message1=Aravind Dekate");
    }

    @Override
    public void run(String... args) throws Exception {
        JobParameters jobParameters = new JobParametersBuilder()
                .addString("message", "MyHello")
                .addDate("date", new Date())
                .addLong("time",System.currentTimeMillis())
                .toJobParameters();
        
        JobExecution execution = jobLauncher.run(job, jobParameters);
        System.out.println("STATUS :: "+execution.getStatus()); 
    }
}

实际输出

2025-04-06T00:16:12.815+05:30  INFO 92963 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
MyHello
null

预期输出中message1应显示为Aravind Dekate,但实际输出为null。

原因分析

Spring Boot 3.x对Spring Batch的自动配置逻辑进行了调整:在2.x版本中,Spring Boot会自动将命令行参数合并到JobParameters中;但3.x版本移除了这一自动行为,命令行参数不会自动加入到JobParameters,需要手动处理。

解决方案

在run方法中,解析命令行参数并添加到JobParametersBuilder中:

修改JobParametersApplication.java的run方法如下:

@Override
public void run(String... args) throws Exception {
    JobParametersBuilder jobParametersBuilder = new JobParametersBuilder()
            .addString("message", "MyHello")
            .addDate("date", new Date())
            .addLong("time", System.currentTimeMillis());

    // 解析命令行参数并添加到JobParameters
    for (String arg : args) {
        String[] keyValue = arg.split("=", 2);
        if (keyValue.length == 2) {
            jobParametersBuilder.addString(keyValue[0], keyValue[1]);
        }
    }

    JobParameters jobParameters = jobParametersBuilder.toJobParameters();
    JobExecution execution = jobLauncher.run(job, jobParameters);
    System.out.println("STATUS :: " + execution.getStatus());
}

验证结果

修改后重新运行,输出将变为:

2025-04-06T00:16:12.815+05:30  INFO 92963 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
MyHello
Aravind Dekate
STATUS :: COMPLETED

内容的提问来源于stack exchange,提问作者PAA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 13:55:55