SpringBatch Reader +豆类注射

时间:2018-12-03 04:47:26

标签: spring spring-boot spring-batch

我正在尝试在Spring Batch Reader中注入存储库。 在一种情况下出现“ NullPonterException”,而在另一种情况下注入。

请仔细检查一下为什么会这样。

案例:1(在阅读器中发生注射的地方)

public class ABCJobLauncher {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Autowired
private JobLauncher jobLauncher;

@Autowired
private Reader reader;

public JobExecution startJobBatchProcessing(KeyRotationParameters keyRotationParameters) {

    JobExecution jobExecution = null;

    reader.setKeyRotationParameters(keyRotationParameters);


    try {

    Step step1 = stepBuilderFactory.get("step1").<BatchParameters, BatchParameters>chunk(1).reader(reader)
                .processor(processor).writer(writer).build();
    Job jobDetails = getJobdetails(keyRotationParameters.getTenantId(), step1);
        JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
                .toJobParameters();

        jobExecution = jobLauncher.run(jobDetails, jobParameters);

    } catch (Exception e) {
        e.printStackTrace();

    }

    return jobExecution;
}
}

,相应的“阅读器”类是

 import org.springframework.batch.item.ItemReader;
 import org.springframework.batch.item.NonTransientResourceException;
 import org.springframework.batch.item.ParseException;
 import org.springframework.batch.item.UnexpectedInputException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;

@Component
public class Reader implements ItemReader<BatchParameters> {

@Autowired
private IRepository repopsitory;


private KeyParameters keyParameters;

public KeyParameters getKeyParameters() {
    return keyParameters;
}

public void setKeyParameters(KeyParameters keyParameters) {
    this.keyParameters = keyParameters;
}

@Override
public BatchParameters read()
        throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {


    VEntry vEntry = repopsitory.findById(id);

    **********(batchParameters)
            *********

    return batchParameters;

}

}

CASE-2 ****(读取器中未发生注入的地方)

假设我为读取器创建新实例,而不是在其中自动装配 “ ABCJobLauncher”。然后在阅读器中没有进行bean注入。

相应的代码是

 @Component
public class ABCJobLauncher {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Autowired
private JobLauncher jobLauncher;



public JobExecution startJobBatchProcessing(KeyRotationParameters keyRotationParameters) {

    JobExecution jobExecution = null;

    Reader reader = new Reader();

    reader.setKeyRotationParameters(keyRotationParameters);
    Processor processor = new Processor();
    Writer writer = new Writer();

    try {



        Step step1 = stepBuilderFactory.get("step1").<BatchParameters, BatchParameters>chunk(1).reader(reader)
                .processor(processor).writer(writer).build();

        Job jobDetails = jobBuilderFactory.get(JobName).incrementer(new RunIdIncrementer()).flow(step1).end().build();
        JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
                .toJobParameters();

        jobExecution = jobLauncher.run(jobDetails, jobParameters);

    } catch (Exception e) {
        e.printStackTrace();

    }

    return jobExecution;
}

2 个答案:

答案 0 :(得分:1)

  

假设我创建了一个新的读取器实例,而不是在“ ABCJobLauncher”中自动装配。那么读取器中不会发生bean注入。

由于手动创建了读取器实例,因此需要手动注入依赖项。因为在这种情况下,读取器不受Spring的管理,因此将不会执行依赖项注入。

因此,在您的“ CASE-2”中,您需要执行以下操作:

IRepository repository = ...; // create instance of IRepository
Reader reader = new Reader();
reader.setRepository(repository); // set the repository
reader.setKeyRotationParameters(keyRotationParameters);

答案 1 :(得分:0)

要完成@Mahmoud响应,您可以像在第一个示例中一样注入存储库,并将其设置为读取器:

@Autowired
private IRepository repopsitory;
@Component
public class ABCJobLauncher {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Autowired
private JobLauncher jobLauncher;  

public JobExecution startJobBatchProcessing(KeyRotationParameters keyRotationParameters) {

    JobExecution jobExecution = null;

    Reader reader = new Reader();
    reader.setRepository(repository); // set the repository
    reader.setKeyRotationParameters(keyRotationParameters);
    Processor processor = new Processor();
    Writer writer = new Writer();

    try {



        Step step1 = stepBuilderFactory.get("step1").<BatchParameters, BatchParameters>chunk(1).reader(reader)
                .processor(processor).writer(writer).build();

        Job jobDetails = jobBuilderFactory.get(JobName).incrementer(new RunIdIncrementer()).flow(step1).end().build();
        JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
                .toJobParameters();

        jobExecution = jobLauncher.run(jobDetails, jobParameters);

    } catch (Exception e) {
        e.printStackTrace();

    }

    return jobExecution;
}