Java Reference
In-Depth Information
Listing 4-30. HelloWorldJob
<beans:bean id="helloWorld" class="com.apress.springbatch.chapter2.HelloWorld/>
<job id="helloWorldJob">
<step id="helloWorldStep">
<tasklet ref="helloWorld"/>
You may be quick to point out that this listing isn't the same as it was in Chapter 2, and you would
be correct. The reason is that you haven't seen one other feature used in Chapter 2: step inheritance.
Step Inheritance
Like jobs, steps can be inherited from each other. Unlike jobs, steps don't have to be abstract to be
inherited. Spring Batch allows you to configure fully defined steps in your configuration and then have
other steps inherit them. Let's start discussing step inheritance by looking at the example used in
Chapter 2, the HelloWorldJob in Listing 4-31.
Listing 4-31. HelloWorldJob
<beans:bean id="helloWorld"
<step id="helloWorldStep">
<tasklet ref="helloWorld"/>
<job id="helloWorldJob">
<step id="step1" parent="helloWorldStep"/>
In Listing 4-31, you configure the tasklet implementation (the helloWorld bean), and then you
configure the step that references the tasklet ( helloWorldStep ). Spring Batch doesn't require that the
step element be nested in a job tag. Once you're defined your step, helloWorldStep , you can then inherit
it when you declare the steps in sequence in your actual job, helloWorldJob . Why would you do this?
In this simple example, there is little benefit to this approach. However, as steps become more
complex, experience shows that it's best to configure your steps outside the scope of your job and then
inherit them with the steps in the job. This allows the actual job declaration to be much more readable
and maintainable.
Obviously, readability isn't the only reason to use inheritance, and that isn't all that is going on even
in this example. Let's dive deeper. In this example, what you're really doing in step1 is inheriting the step
helloWorldStep and all its attributes. However, step1 chooses not to override any of them.
Step inheritance provides a more complete inheritance model than that of job inheritance. In step
inheritance you can fully define a step, inherit the step, and then add or override any of the values you
wish. You can also declare a step abstract and place only common attributes there.
Listing 4-32 shows an example of how steps can add and override attributes configured in their
parent. You start with the parent step, vehicleStep , which declares a reader, writer, and commit-
Search WWH ::

Custom Search