英文:
Set IteamReader input from ExecutionContext
问题
我的Spring Batch中的第一步实际上是从服务器下载CSV文件,为它分配一个动态名称,并将其存储在ExecutionContext中。
现在的问题是,在bean创建时如何将输入文件的名称传递给FlatFileItemReader。
例如:
@Bean
public FlatFileItemReader<Customer> customerItemReader() {
FlatFileItemReader<Customer> reader = new FlatFileItemReader<>();
reader.setLinesToSkip(1);
reader.setResource(new ClassPathResource("/data/customer.csv"));
DefaultLineMapper<Customer> customerLineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setNames(new String[] {"id", "firstName", "lastName", "birthdate"});
customerLineMapper.setLineTokenizer(tokenizer);
customerLineMapper.setFieldSetMapper(new CustomerFieldSetMapper());
customerLineMapper.afterPropertiesSet();
reader.setLineMapper(customerLineMapper);
return reader;
}
请注意,reader.setResource(new ClassPathResource("/data/customer.csv"))
是在创建bean时设置的。
如何将信息从ExecutionContext传递给FlatFileItemReader作为输入源?
我是否需要查找Spring Batch表?
英文:
My first step in Spring Batch actually downloads a CSV from a server and assigns it a dynamic name and stores it in the ExecutionContext.
Now the question how do I pass the name of the input file to the FlatFileItemReader at the time of bean creation.
Take for example:
@Bean
public FlatFileItemReader<Customer> customerItemReader() {
FlatFileItemReader<Customer> reader = new FlatFileItemReader<>();
reader.setLinesToSkip(1);
reader.setResource(new ClassPathResource("/data/customer.csv"));
DefaultLineMapper<Customer> customerLineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setNames(new String[] {"id", "firstName", "lastName", "birthdate"});
customerLineMapper.setLineTokenizer(tokenizer);
customerLineMapper.setFieldSetMapper(new CustomerFieldSetMapper());
customerLineMapper.afterPropertiesSet();
reader.setLineMapper(customerLineMapper);
return reader;
}
Note that reader.setResource(new ClassPathResource("/data/customer.csv"))
is set at the time when the bean is created.
How do I pass the information from the ExecutionContext
to the FlatFileItemReader
as the input source ?
Do I need to lookup the Spring batch tables ?
答案1
得分: 1
Make your bean @StepScope
or @JobScope
and use @Value
to get the value.
@Bean
@StepScope
public FlatFileItemReader<Customer> customerItemReader(@Value("#{stepExecutionContext['your.name']}") String file) {
// Bean creation logic here.
}
Now if you would have taken the time to read the documentation you would have read it yourself.
英文:
Make your bean @StepScope
or @JobScope
and use @Value
to get the value.
@Bean
@StepScope
public FlatFileItemReader<Customer> customerItemReader(@Value("#{stepExecutionContext['your.name']} String file) {
// Bean creation logic here.
}
Now if you would have taken the time to read the documentation you would have read it yourself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论