85 lines
8.8 KiB
HTML
85 lines
8.8 KiB
HTML
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg"><head><title>Late Binding of Job and Step Attributes</title><link rel="stylesheet" type="text/css" href="docbook-epub.css"/><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"/><link rel="prev" href="ch05s03.xhtml" title="Controlling Step Flow"/><link rel="next" href="ch06.xhtml" title="Chapter 6. ItemReaders and ItemWriters"/></head><body><header/><section class="section" title="Late Binding of Job and Step Attributes" epub:type="subchapter" id="late-binding"><div class="titlepage"><div><div><h2 class="title" style="clear: both">Late Binding of Job and Step Attributes</h2></div></div></div><p>Both the XML and Flat File examples above use the Spring
|
||
<code class="classname">Resource</code> abstraction to obtain a file. This works
|
||
because <code class="classname">Resource</code> has a <span class="markup">getFile</span>
|
||
method, which returns a <code class="classname">java.io.File</code>. Both XML and
|
||
Flat File resources can be configured using standard Spring
|
||
constructs:</p><pre class="programlisting"><bean id="flatFileItemReader"
|
||
class="org.springframework.batch.item.file.FlatFileItemReader">
|
||
<property name="resource"
|
||
value="file://outputs/20070122.testStream.CustomerReportStep.TEMP.txt" />
|
||
</bean></pre><p>The above <code class="classname">Resource</code> will load the file from
|
||
the file system location specified. Note that absolute locations have to
|
||
start with a double slash ("//"). In most spring applications, this
|
||
solution is good enough because the names of these are known at compile
|
||
time. However, in batch scenarios, the file name may need to be determined
|
||
at runtime as a parameter to the job. This could be solved using '-D'
|
||
parameters, i.e. a system property:</p><pre class="programlisting"><bean id="flatFileItemReader"
|
||
class="org.springframework.batch.item.file.FlatFileItemReader">
|
||
<property name="resource" value="${input.file.name}" />
|
||
</bean></pre><p>All that would be required for this solution to work would be a
|
||
system argument (-Dinput.file.name="file://file.txt"). (Note that although
|
||
a <code class="classname">PropertyPlaceholderConfigurer</code> can be used here,
|
||
it is not necessary if the system property is always set because the
|
||
<code class="classname">ResourceEditor</code> in Spring already filters and does
|
||
placeholder replacement on system properties.)</p><p>Often in a batch setting it is preferable to parameterize the file
|
||
name in the <a class="link" href=""><code class="classname">JobParameters</code></a> of the
|
||
job, instead of through system properties, and access them that way. To
|
||
accomplish this, Spring Batch allows for the late binding of various Job
|
||
and Step attributes:</p><pre class="programlisting"><bean id="flatFileItemReader" scope="step"
|
||
class="org.springframework.batch.item.file.FlatFileItemReader">
|
||
<property name="resource" value="<span class="bold"><strong>#{jobParameters['input.file.name']}</strong></span>" />
|
||
</bean></pre><p>Both the <code class="classname">JobExecution</code> and
|
||
<code class="classname">StepExecution</code> level
|
||
<code class="classname">ExecutionContext</code> can be accessed in the same
|
||
way:</p><pre class="programlisting"><bean id="flatFileItemReader" scope="step"
|
||
class="org.springframework.batch.item.file.FlatFileItemReader">
|
||
<property name="resource" value="<span class="bold"><strong>#{jobExecutionContext['input.file.name']}</strong></span>" />
|
||
</bean></pre><pre class="programlisting"><bean id="flatFileItemReader" scope="step"
|
||
class="org.springframework.batch.item.file.FlatFileItemReader">
|
||
<property name="resource" value="<span class="bold"><strong>#{stepExecutionContext['input.file.name']}</strong></span>" />
|
||
</bean></pre><div class="note" title="Note" epub:type="notice"><table style="border: 0; "><tr><td style="text-align: center; vertical-align: top; width: 25; " rowspan="2"><img alt="[Note]" src="images/note.png"/></td><th style="text-align: left; ">Note</th></tr><tr><td style="text-align: left; vertical-align: top; "><p>Any bean that uses late-binding must be declared with
|
||
scope="step". See for <a class="xref" href="ch05s04.xhtml#step-scope" title="Step Scope">the section called “Step Scope”</a> more
|
||
information.</p></td></tr></table></div><div class="note" title="Note" epub:type="notice"><table style="border: 0; "><tr><td style="text-align: center; vertical-align: top; width: 25; " rowspan="2"><img alt="[Note]" src="images/note.png"/></td><th style="text-align: left; ">Note</th></tr><tr><td style="text-align: left; vertical-align: top; "><p>If you are using Spring 3.0 (or above) the expressions in
|
||
step-scoped beans are in the Spring Expression Language, a powerful
|
||
general purpose language with many interesting features. To provide
|
||
backward compatibility, if Spring Batch detects the presence of older
|
||
versions of Spring it uses a native expression language that is less
|
||
powerful, and has slightly different parsing rules. The main difference
|
||
is that the map keys in the example above do not need to be quoted with
|
||
Spring 2.5, but the quotes are mandatory in Spring 3.0.</p></td></tr></table></div><section class="section" title="Step Scope" epub:type="division" id="step-scope"><div class="titlepage"><div><div><h3 class="title">Step Scope</h3></div></div></div><p>All of the late binding examples from above have a scope of "step"
|
||
declared on the bean definition:</p><pre class="programlisting"><bean id="flatFileItemReader" <span class="bold"><strong>scope="step"</strong></span>
|
||
class="org.springframework.batch.item.file.FlatFileItemReader">
|
||
<property name="resource" value="#{jobParameters[input.file.name]}" />
|
||
</bean></pre><p>Using a scope of <code class="classname">Step</code> is required in order
|
||
to use late binding since the bean cannot actually be instantiated until
|
||
the <code class="classname">Step</code> starts, which allows the attributes to
|
||
be found. Because it is not part of the Spring container by default, the
|
||
scope must be added explicitly, either by using the
|
||
<code class="literal">batch</code> namespace:</p><pre class="programlisting"><beans xmlns="http://www.springframework.org/schema/beans"
|
||
xmlns:batch="http://www.springframework.org/schema/batch"
|
||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||
xsi:schemaLocation="...">
|
||
<batch:job .../>
|
||
...
|
||
</beans></pre><p>or by including a bean definition explicitly for the<code class="classname">
|
||
StepScope</code> (but not both):</p><pre class="programlisting"><bean class="org.springframework.batch.core.scope.StepScope" /></pre></section><section class="section" title="Job Scope" epub:type="division" id="job-scope"><div class="titlepage"><div><div><h3 class="title">Job Scope</h3></div></div></div><p>Job scope, introduced in Spring Batch 3.0 is similar to Step scope
|
||
in configuration but is a Scope for the Job context so there is only one
|
||
instance of such a bean per executing job. Additionally, support is provided
|
||
for late binding of references accessible from the JobContext using
|
||
#{..} placeholders. Using this feature, bean properties can be pulled from
|
||
the job or job execution context and the job parameters. E.g.
|
||
</p><pre class="programlisting"><bean id="..." class="..." <span class="bold"><strong>scope="job"</strong></span>>
|
||
<property name="name" value="#{jobParameters[input]}" />
|
||
</bean>
|
||
</pre><pre class="programlisting"><bean id="..." class="..." <span class="bold"><strong>scope="job"</strong></span>>
|
||
<property name="name" value="#{jobExecutionContext['input.name']}.txt" />
|
||
</bean>
|
||
</pre><p>Because it is not part of the Spring container by default, the scope
|
||
must be added explicitly, either by using the <code class="literal">batch</code> namespace:</p><pre class="programlisting"><beans xmlns="http://www.springframework.org/schema/beans"
|
||
xmlns:batch="http://www.springframework.org/schema/batch"
|
||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||
xsi:schemaLocation="...">
|
||
|
||
<batch:job .../>
|
||
...
|
||
</beans></pre><p>Or by including a bean definition explicitly for the <code class="classname">JobScope</code> (but not both):</p><pre class="programlisting"><bean class="org.springframework.batch.core.scope.JobScope" /></pre></section></section><footer/></body></html> |