英文:
Spring batch jobParameters value null
问题
在一个Spring批处理中,我试图传递jobParameters
并在我的JdbcCursorItemReader
中使用它们。下面是相关的一部分代码:
@Configuration
public class SpringBatchConfig extends DefaultBatchConfigurer{
private String demoVar;
public String getDemoVar() {
return demoVar;
}
public void setDemoVar(@Value("#{jobParameters['demoVarValue']}") String demoVar) {
this.demoVar = demoVar;
}
@Bean
@StepScope
public JdbcCursorItemReader<User> reader1() {
String sqlQuery = String.format("select * FROM TABLE_NAME where demoVar=? ");
System.out.println(demoVar);
JdbcCursorItemReader<User> cursorItemReader = new JdbcCursorItemReader<>();
cursorItemReader.setDataSource(dataSource);
cursorItemReader.setSql(sqlQuery);
cursorItemReader.setPreparedStatementSetter(new PreparedStatementSetter() {
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, demoVar);
}
});
cursorItemReader.setRowMapper(new DbRowMapper());
return cursorItemReader;
}
...
控制器类(包含作业启动器):
@RestController
@RequestMapping("/load")
public class LoadController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@GetMapping("/users")
public BatchStatus load() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
JobParameters jobParameters = new JobParametersBuilder()
.addString("demoVarValue", "001")
.toJobParameters();
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
System.out.println("JobExecution: " + jobExecution.getStatus());
return jobExecution.getStatus();
}
}
但是在reader
bean中,从jobParameters
解析出的demoVar
值传递为null
,而不是预期的001
。
英文:
In a spring batch, I'm trying to pass jobParameters
and use them in my JdbcCursorItemReader
. A snippet from the same is:
@Configuration
public class SpringBatchConfig extends DefaultBatchConfigurer{
private String demoVar;
public String getDemoVar() {
return demoVar;
}
public void setDemoVar(@Value("#{jobParameters['demoVarValue']}") String demoVar) {
this.demoVar = demoVar;
}
@Bean
@StepScope
public JdbcCursorItemReader<User> reader1() {
String sqlQuery = String.format("select * FROM TABLE_NAME where demoVar=? ");
System.out.println(demoVar);
JdbcCursorItemReader<User> cursorItemReader = new JdbcCursorItemReader<>();
cursorItemReader.setDataSource(dataSource);
cursorItemReader.setSql(sqlQuery);
cursorItemReader.setPreparedStatementSetter(new PreparedStatementSetter() {
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, demoVar);
}
});
cursorItemReader.setRowMapper(new DbRowMapper());
return cursorItemReader;
}
...
Controller class(containing job launcher):
@RestController
@RequestMapping("/load")
public class LoadController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@GetMapping("/users")
public BatchStatus load() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
JobParameters jobParameters = new JobParametersBuilder()
.addString("demoVarValue", "001")
.toJobParameters();
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
System.out.println("JobExecution: " + jobExecution.getStatus());
return jobExecution.getStatus();
}
}
But in the reader
bean, the value of the demoVar
parsed from jobParameters
is being passed as null
instead of the expected 001
.
答案1
得分: 3
除非在其他地方调用了getter方法,否则您不需要`demoVar`字段。您可以将其作为reader的bean定义方法的参数传递:
@Bean
@StepScope
public JdbcCursorItemReader<User> reader1(@Value("#{jobParameters['demoVarValue']}") String demoVar) {
String sqlQuery = String.format("select * FROM TABLE_NAME where demoVar=? ");
System.out.println(demoVar);
JdbcCursorItemReader<User> cursorItemReader = new JdbcCursorItemReader<>();
cursorItemReader.setDataSource(dataSource);
cursorItemReader.setSql(sqlQuery);
cursorItemReader.setPreparedStatementSetter(new PreparedStatementSetter() {
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, demoVar);
}
});
cursorItemReader.setRowMapper(new DbRowMapper());
return cursorItemReader;
}
作业参数的延迟绑定在参考文档的[作业和步骤属性的延迟绑定](https://docs.spring.io/spring-batch/docs/4.2.x/reference/html/step.html#late-binding)部分进行了解释。
英文:
You don't need the demoVar
field (unless you are calling the getter somewhere else). You can pass it as a parameter of reader's bean definition method:
@Bean
@StepScope
public JdbcCursorItemReader<User> reader1(@Value("#{jobParameters['demoVarValue']}") String demoVar) {
String sqlQuery = String.format("select * FROM TABLE_NAME where demoVar=? ");
System.out.println(demoVar);
JdbcCursorItemReader<User> cursorItemReader = new JdbcCursorItemReader<>();
cursorItemReader.setDataSource(dataSource);
cursorItemReader.setSql(sqlQuery);
cursorItemReader.setPreparedStatementSetter(new PreparedStatementSetter() {
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, demoVar);
}
});
cursorItemReader.setRowMapper(new DbRowMapper());
return cursorItemReader;
}
Late binding of job parameters is explained in the Late Binding of Job and Step Attributes section of the reference documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论