I am implementing a custom ItemProcessor<I, O> in spring batch for processing data from a Rest api .
I want access some values from jobParameter inside my ItemProcessor class . Any suggestion on how to do that ?
In Tasklet we can access JobParameter but not sure how to do in ItemProcessor .
MyItemProcessor.java
@Component
public class MyItemProcessor implements ItemProcessor<User, UserDetails> {
@Override
public UserDetails process(User user) throws Exception {
// access values from job parameter here
return null;
}
}
You can make your item processor step-scoped and inject job parameters in it. The following is one way of doing that:
You could also inject a specific parameter if you want with something like the following:
Since field injection is not recommended, you can inject job parameters in your item processor when you define it as a bean, something like:
Once that in place, you can declare your item processor bean as follows:
Fore more details about scoped beans, please check the documentation here: Late Binding of Job and Step attributes.