我想实现项目阅读器和项目编写者的后期绑定。应动态配置读写器。
JobController.java
@RestController
@RequestMapping("/job")
public class JobController {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job customJob;
@RequestMapping(value = "/cust", method = RequestMethod.GET)
public void runCustJob() {
JobParametersBuilder jobBuilder =
new JobParametersBuilder();
jobBuilder.addLong("time",System.currentTimeMillis());
jobBuilder.addString("reader", "fileToFileReader");
jobBuilder.addString("writer", "fileToFileWriter");
try {
jobLauncher.run(customJob, jobBuilder.toJobParameters());
} catch (Exception e) {
e.printStackTrace();
}
}
}
JobConfig.java
@Configuration
@EnableBatchProcessing
public class JobConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private Environment env;
@Autowired
private ApplicationContext ac;
@Bean
@StepScope
public FileToFileReader fileToFileReader()//@Value("# {jobParameters['filePath']}") String filePath
{
//configure flatfileitemreader
return reader;
}
@Bean
@StepScope
public FileToFileWriter fileToFileWriter()
{
//configure flatfileitemwriter
return writer;
}
@Bean
public Job customJob(@Value("#{jobParameters['reader']}") String reader, @Value("#{jobParameters['writer']}") String writer)//@Value("#{jobParameters['reader']}") String reader, @Value("#{jobParameters['writer']}") String writer
{
return jobBuilderFactory.get("customJob")
.incrementer(new RunIdIncrementer())
.flow(processFile1(reader, writer))
.end()
.build();
}
@Bean
public Step processFile1(String reader, String writer) {
return stepBuilderFactory.get("processFile1").allowStartIfComplete(true)
.<Person, Person> chunk(100)
.reader((ItemReader<? extends Person>) ac.getBean(reader))
.writer((ItemWriter<? super Person>) ac.getBean(writer))
.build();
}
}
我想在JobController.java中将bean名称设置为jobParameters,并在JobConfig.java中按名称访问bean。我试过@JobScope和@StepScope,但是没有用,或者我可能不适当地使用了这些示波器。
请告诉我如何实现后期绑定。