简体   繁体   English

如何从Spring Batch中访问Bean集

[英]How to access a bean set from spring batch

How can I retrieve a java object which is created from reader (Custom reader in my case) from writer? 我该如何检索从writer的读者(在本例中为Custom读者)创建的java对象?

I have to keep count of the records processed or skipped due to exception and all for mail notification after job execution. 由于工作异常,我必须保留由于异常而count of the records processedskipped count of the records processed并在作业执行后全部用于邮件通知。 I don't want to put it in execution context as I don't need to persist it. 我不想将其放在execution context因为我不需要将其持久化。

Can I just set a bean from reader and autowire it from writer and use it ? 我可以只设置读取器中的bean并autowire写入器中的autowire并使用它吗?

You can simply use same class as reader , writer and then a instance variable can be shared. 您可以简单地使用与reader,writer相同的类,然后可以共享一个实例变量。 In fact you use same object for doing stuffs like sending mail by implementing StepExecutionListener in '@afterJob' method after completion of job. 实际上,您使用相同的对象来完成诸如发送邮件之类的工作,该任务通过在作业完成后以'@afterJob'方法实现StepExecutionListener来实现。

This is good approach if you reader and writer are having simple code. 如果您的读者和作家都拥有简单的代码,那么这是个好方法。 Or you can delegate that code to other class by having separater reader and writer class having actual logic and you can pass your instance variable to those classes. 或者,您可以通过使分隔符读取器和写入器类具有实际的逻辑,将该代码委托给其他类,然后可以将实例变量传递给这些类。 You just need to define same class as reader , writer and listener in your configuration. 您只需要在配置中定义与reader,writer和listener相同的类。

 public class CommitWriter implements ItemWriter<myObject>, ItemReader<myObject> , StepExecutionListener{

private myPojo sharedObject;

//Read method goes here
 public myObjectread() throws Exception{

 }
// Write method goes here
public void write(List<? extends myObject> arg0)
throws Exception {

}


 //After job
 @Override
public ExitStatus afterStep(StepExecution stepExecution) {

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM