Search code examples
springjavabeansspring-batchfactoryjobs

How to define templates for Spring Batch jobs?


I'm using Spring Batch 2.1.5. I have many jobs that are very similar between each other and I'm looking for a way to have an smaller XML acting as a job template.

The things that are shared among jobs are readers, processors, writers and one tasklet. Also some of the parameters for each of these beans are the same. For instance they all use the same data source.

I thought about 4 approaches and 3 of them don't work...

1 - Using a postprocessor to add the common beans and attributes as default values is not possible, because the Spring Batch class JobParserJobFactoryBean is not a public class.

2 - To add an XML extension seems to be a wrong thing, because I'm not adding any custom tags to the XML file.

3 - Using a PropertyOverrideConfigurer I can put default values into properties, but I have to define those values for each bean. So I'll have many repeated values and I'll only move the problem to properties.

4 - Using some kind of custom factory bean. These seems to be the only choice, but I don't know exactly how to plug it into the existing code.

Did anybody try to do this? Can somebody give tips or recommend resources on how to do it?


Solution

  • Spring Batch provides the ability to define Abstract Jobs (and steps, etc.) to inherit from. Take a look at the reference manual for more information. Using this you should be able to accomplish exactly what you are looking for.