我有一个带有PreparedStatementSetter的Spring批处理,用于查询阅读器步骤。我想从之前保存到ExecutionContext中的PreparedStatementSetter访问全局数据。
我如何从这个PreparedStatementSetter访问ExecutionContext?
@Component
public class CurrentBatchIdPreparedStatementSetter implements PreparedStatementSetter
{
@Override
public void setValues(PreparedStatement preparedStatement) throws SQLException
{
// how to access to ExecutionContext here ?
}
}
谢谢
我们可以
最佳答案
如果范围是step,我们可以从步骤执行中获取ExecutionContext。
@Component
public class CurrentBatchIdPreparedStatementSetter implements PreparedStatementSetter
{
@Value("#{stepExecution}")
private StepExecution stepExecution;
@Override
public void setValues(PreparedStatement preparedStatement) throws SQLException
{
final ExecutionContext executionContext = stepExecution.getExecutionContext();
//Do your operations here
}
}
您也可以从作业执行上下文中获得相同的信息。
@Value("#{jobExecution}")
private JobExecution jobExecution;
//inside method
final ExecutionContext executionContext = jobExecution.getExecutionContext();
或者您可以轻松地从作业执行上下文中获取价值。
@Value ( "# {jobExecutionContext ['param1']}" )
private String param1;
@Value ( "# {jobExecutionContext ['param2']}" )
private String param2;