【问题标题】:Spring batch jobParameters not working in java configSpring批处理jobParameters在java配置中不起作用
【发布时间】:2018-07-06 10:51:59
【问题描述】:

我正在尝试运行批处理作业,我想让我的 SQL 查询动态化。但是我在构建代码时遇到了一个异常“在'org.springframework.beans.factory.config.BeanExpressionContext'类型的对象上找不到属性或字段'jobParameters' - 可能不是公共的或无效的?” 。以下是相同的代码 sn-ps 和异常跟踪。

批量配置

@Configuration
@EnableBatchProcessing
public class StoreSalesBatchConfiguration {

@Autowired
@Qualifier("jobBuilderFactory")
private JobBuilderFactory jobBuilderFactory;

@Autowired
@Qualifier("stepBuilderFactory")
private StepBuilderFactory stepBuilderFactory;

@Autowired
@Qualifier("jobCompletionNotificationListener")
private JobCompletionNotificationListener jobCompletionNotificationListener;

@Autowired
@Qualifier("jobLauncher")
private JobLauncher jobLauncher;

@Autowired
@Qualifier("storeSalesJob")
private Job storeSalesJob;

@Autowired
private GameStoreSalesRepository storeSalesRepository;  

@Bean
@StepScope
ItemReader<GameStoreSales> gameStoreSalesReader(@Qualifier("gdwMpsBatch") final DataSource dataSource,
        @Value("#{jobParameters[maxDate]}") String maxDate) {
    JdbcCursorItemReader<GameStoreSales> databaseReader = new JdbcCursorItemReader<>();
    databaseReader.setDataSource(dataSource);
    databaseReader.setSql(CommonConstants.STORE_SALES_QUERY);
    databaseReader.setPreparedStatementSetter(new PreparedStatementSetter() {

        @Override
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setString(1, maxDate);
        }
    });
    databaseReader.setRowMapper(new BeanPropertyRowMapper<>(GameStoreSales.class));
    return databaseReader;
}

@Bean
public GameStoreSalesProcessor gameStoreSalesProcessor() {
    return new GameStoreSalesProcessor();
}

@Bean
public ItemWriter<GameStoreSales> gameStoreSalesWriter() throws Exception {
    return new GameStoreSalesWriter();
}

@Bean
public Step gameStoreSalesStep(@Qualifier("gdwMpsBatch") final DataSource dataSource,
                            @Value("#{jobParameters[maxDate]}") String maxDate) throws Exception {
    return stepBuilderFactory.get("gameStoreSalesStep").<GameStoreSales, GameStoreSales>chunk(1000)
            .reader(gameStoreSalesReader(dataSource,maxDate)).processor(gameStoreSalesProcessor()).writer(gameStoreSalesWriter()).build();
}

@Bean(name = "storeSalesJob")
public Job storeSalesJob(Step gameStoreSalesStep) {
    return jobBuilderFactory.get("storeSalesJob").incrementer(new RunIdIncrementer())
            .listener(jobCompletionNotificationListener).flow(gameStoreSalesStep).end().build();
}

@Scheduled(cron = "*/30 * * * * *")
public void runStoreSalesJob() throws JobExecutionAlreadyRunningException, JobRestartException,
        JobInstanceAlreadyCompleteException, JobParametersInvalidException {
    String dateParam = new Date().toString();
    String maxDate = storeSalesRepository.getMaxCalDate();
    JobParameters param = new JobParametersBuilder().addString("date", dateParam)
                                                    .addString("maxDate", weekEnd)
                                                    .toJobParameters();
    try{
        jobLauncher.run(storeSalesJob, param);
    } catch(Exception e){
        e.printStackTrace();
    }
}
}

异常跟踪:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'storeSalesBatchConfiguration': Unsatisfied dependency expressed through field 'storeSalesJob'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'storeSalesJob' defined in class path resource [com/staples/mpsbatch/config/StoreSalesBatchConfiguration.class]: Unsatisfied dependency expressed through method 'storeSalesJob' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gameStoreSalesStep' defined in class path resource [com/staples/mpsbatch/config/StoreSalesBatchConfiguration.class]: Unsatisfied dependency expressed through method 'gameStoreSalesStep' parameter 1; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'storeSalesJob' defined in class path resource [com/staples/mpsbatch/config/StoreSalesBatchConfiguration.class]: Unsatisfied dependency expressed through method 'storeSalesJob' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gameStoreSalesStep' defined in class path resource [com/staples/mpsbatch/config/StoreSalesBatchConfiguration.class]: Unsatisfied dependency expressed through method 'gameStoreSalesStep' parameter 1; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'gameStoreSalesStep' defined in class path resource [com/staples/mpsbatch/config/StoreSalesBatchConfiguration.class]: Unsatisfied dependency expressed through method 'gameStoreSalesStep' parameter 1; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?

Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is 

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?

请说明为什么jobParameters没有被识别以及配置出错的地方。

【问题讨论】:

  • 您是否尝试过在参数名称周围添加简单的引号?喜欢:@Value("#{jobParameters['maxDate']}")
  • 是的,我做到了。但我得到了同样的例外。它似乎根本不识别jobParameters。
  • 我在之前的评论中给出的语法是正确的。有关详细信息,请参见此处:docs.spring.io/spring-batch/4.0.x/reference/html/… 您的 bean 使用 @StepScope 进行注释。这是正确的。我没有看到您的代码有任何问题。为什么您的项目阅读器 bean 的方法 gameStoreSalesReader 不公开?您是否尝试过像其他人一样公开它?
  • @SonamBhardwaj 你找到问题的根源了吗?或可能的解决方案?我也面临同样的问题。

标签: java spring spring-batch jobs


【解决方案1】:

弹簧批处理 StepScope 对象是特定步骤的唯一对象,而不是单例对象。您可能知道,Spring 中的默认 bean 作用域是单例。但是通过将 Spring Batch 组件指定为 StepScope 意味着 Spring Batch 将使用 spring 容器为每个步骤执行实例化该组件的新实例。

这对于执行参数后期绑定通常很有用,其中可以在 StepContext 或 JobExecutionContext 级别指定参数并且需要替换占位符,就像您的示例具有文件名要求一样。

使用 StepScope 的另一个有用的原因是当您决定在并行步骤中重用相同的组件时。如果组件管理任何内部状态,那么它基于 StepScope 很重要,这样一个线程不会损害由另一个线程管理的状态(例如,给定步骤的每个线程都有自己的 StepScope 组件实例)。

尝试使用 StepScope 进行注释:

    @Bean
    @StepScope
    public Step gameStoreSalesStep(@Qualifier("gdwMpsBatch") final DataSource dataSource,
                                @Value("#{jobParameters[maxDate]}") String maxDate) throws Exception {
        return stepBuilderFactory.get("gameStoreSalesStep").<GameStoreSales, GameStoreSales>chunk(1000)
                .reader(gameStoreSalesReader(dataSource,maxDate)).processor(gameStoreSalesProcessor()).writer(gameStoreSalesWriter()).build();
    }

【讨论】:

    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 2022-07-19
    • 2018-03-08
    • 2017-04-24
    • 2015-04-01
    • 2023-03-27
    相关资源
    最近更新 更多