无法在 ItemStreamReader 打开方法中自动装配对象

Could not autowired Object in ItemStreamReader open method

我使用 Spring 批处理 Spring 引导,这是我的主要 class。

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

这是我的配置classes

@Configuration
public class AppConfig {

    @Bean
    public MyObject getObject() {
        return new MyObject();
    }
}

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    private static final String OVERRIDDEN_BY_EXPRESSION = null;

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

    @Bean
    public Job job() {
        return jobs.get(Constants.FULL_JOB_NAME)
                .start(stepProcessDocument())
                .build();
    }

    @Bean
    protected Step stepProcessDocument() {
        return steps.get(Constants.STEP_PROCESS_DOCUMENT_NAME)
            .<Document,Document>chunk(10)
            .reader(buildItemReader(OVERRIDDEN_BY_EXPRESSION))
            .processor(buildItemProcessor())
            .writer(buildItemWriter(OVERRIDDEN_BY_EXPRESSION))
            .build();
    }

    @Bean
    @StepScope
    protected ItemReader<Document> buildItemReader(@Value("#{jobParameters[" + Constants.PARAM_JOB_PARAMETER + "]}") String param) {
        ItemStreamReader<Document> reader = new CustomItemReader(param);
        reader.open(new ExecutionContext());
        return reader;
    }

    @Bean
    protected ItemProcessor<Document, Document> buildItemProcessor() {
        return new CustomItemProcessor();
    }

    @Bean
    @StepScope
    protected ItemWriter<Document> buildItemWriter(@Value("#{jobParameters[" + Constants.PARAM_JOB_PARAMETER + "]}") String param) { 
        ItemStreamWriter<Document> writer = new CustomItemWriter(param);
        writer.open(new ExecutionContext());
        return writer;
    }

    @Bean
    public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) {
        JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
        jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
        return jobRegistryBeanPostProcessor;
    }
}

这是我在我的应用程序中使用的自定义文件reader。

public class CustomItemReader implements ItemStreamReader<Document> {

@Autowired
private MyObject myObject;

private int count = 0;
private String param;

public CustomItemReader(String param) {
    this.param = param;
}

@Override
public void open(ExecutionContext executionContext)
        throws ItemStreamException {
    myObject.open(); //myObject is null
}

@Override
public void update(ExecutionContext executionContext)
        throws ItemStreamException {
    // TODO Auto-generated method stub

}

@Override
public void close() throws ItemStreamException {
    myObject.close();
}

@Override
public Document read() throws Exception, UnexpectedInputException,
        ParseException, NonTransientResourceException {
    myObject.doStuff();
    count++;
    if(count == 5) {
        return null;
    }
    return new Document();
}

我在 myObject 上遇到 Java 空指针异常。 为什么我无法在 ItemStreamReader 打开方法中自动装配 java 对象?

您需要将 Bean 作为上下文的一部分加载,因为您使用的是 Spring 引导,所以您可以使用如下内容:

@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) throws Exception {
        // System.exit is common for Batch applications since the exit code can be used to
        // drive a workflow
        System.exit(SpringApplication.exit(SpringApplication.run(
                Application.class, args)));
    }
}

@ComponentScan 将注册所有 Spring 组件,如 @Component, @Repository, @Service, @Controller 包括 @Configuration 类。如果您需要查看完整示例,请查看此项目:http://www.codingpedia.org/ama/spring-batch-tutorial-with-spring-boot-and-java-configuration/

希望对您有所帮助。

我找到了答案。在我的 itemReader 的定义方法中:

@Bean
@StepScope
protected ItemReader<Document> buildItemReader(@Value("#{jobParameters[" + Constants.PARAM_JOB_PARAMETER + "]}") String param) {
    ItemStreamReader<Document> reader = new CustomItemReader(param);
    reader.open(new ExecutionContext());
    return reader;
}

我执行方法:

reader.open(new ExecutionContext());

因此,每次我重新启动使用此 itemReader 的失败作业时,我都会破坏失败作业步骤的执行上下文。我无法从中断的地方继续。

要解决这个问题,我必须删除这行代码并 return 一个 ItemStreamReader。这样框架就可以访问open方法了

@Bean
@StepScope
protected ItemStreamReader<Document> buildItemReader(@Value("#{jobParameters[" + Constants.PARAM_JOB_PARAMETER + "]}") String param) {
    ItemStreamReader<Document> reader = new CustomItemReader(param);
    return reader;
}

此外,此解决方案也解决了我原来的问题。但不幸的是,我不知道为什么,因为我是 Spring 框架的初学者。

希望有人能帮我理解。