INT-2491: Scripting: Variables Improvements

JIRA: https://jira.springsource.org/browse/INT-2491

* Add support variables for 'inline' scripts
* Add convenient attribute `variables` for script tags
* Introduce `BeanFactoryFallbackBinding` for groovy scripts.

INT-2491: Polishing

* Add check for duplication of provided variables
* Tests and Docs

Polishing
This commit is contained in:
Artem Bilan
2013-11-18 17:28:29 +02:00
committed by Gary Russell
parent a7cabc53b0
commit 87798b055f
14 changed files with 327 additions and 143 deletions

View File

@@ -57,9 +57,26 @@
Setting a custom GroovyObjectCustomizer is not mutually exclusive with <code>&lt;variable&gt;</code> sub-elements or
the <code>script-variable-generator</code> attribute. It can also be provided when defining an inline script.
For more information regarding <code>&lt;variable&gt;</code> and <code>script-variable-generator</code>, see the
paragraph '<emphasis>Script variable bindings</emphasis>' of <xref linkend="scripting-config"/>.
</para>
</para>
<para>
With <emphasis>Spring Integration 3.0</emphasis>, in addition to the <code>variable</code> sub-element,
the <code>variables</code> attribute has been introduced. Also, groovy scripts have the ability to resolve a
variable to a bean in the
<interfacename>BeanFactory</interfacename>, if a binding variable was not provided with
the name:
<programlisting language="xml">&lt;int-groovy:script&gt;
&lt;![CDATA[
entityManager.persist(payload)
payload
]]&gt;
&lt;/int-groovy:script&gt;</programlisting>
where variable <code>entityManager</code> is an appropriate bean in the application context.
</para>
<para>
For more information regarding <code>&lt;variable&gt;</code>, <code>variables</code>,
and <code>script-variable-generator</code>, see the
paragraph '<emphasis>Script variable bindings</emphasis>' of <xref linkend="scripting-config"/>.
</para>
</section>
<section id="groovy-control-bus">

View File

@@ -98,7 +98,32 @@
</script:script>]]></programlisting>
As shown in the above example, you can bind a script variable either to a scalar value or a Spring bean reference. Note that
<code>payload</code> and <code>headers</code> will still be included as binding variables.
</para>
<para>
With <emphasis>Spring Integration 3.0</emphasis>, in addition to the <code>variable</code> sub-element,
the <code>variables</code> attribute has been introduced. This attribute and <code>variable</code> sub-elements
aren't mutually exclusive and you can combine them within one <code>script</code> component. However variables must
be unique, regardless of where they are defined. Also, since <emphasis>Spring Integration 3.0</emphasis>,
variable bindings are allowed for inline scripts too:
<programlisting language="xml">&lt;service-activator input-channel="input"&gt;
&lt;script:script lang="ruby" variables="foo=FOO, date-ref=dateBean"&gt;
&lt;script:variable name="bar" ref="barBean"/&gt;
&lt;script:variable name="baz" value="bar"/&gt;
&lt;![CDATA[
payload.foo = foo
payload.date = date
payload.bar = bar
payload.baz = baz
payload
]]&gt;
&lt;/script:script&gt;
&lt;/service-activator&gt;</programlisting>
The example above shows a combination of an inline script, a <code>variable</code> sub-element and a <code>variables</code> attribute.
The <code>variables</code> attribute is a comma-separated value, where each segment contains an '=' separated pair
of the variable and its value. The variable name can be suffixed with <code>-ref</code>, as in the
<code>date-ref</code> variable above. That means that the binding variable
will have the name <code>date</code>, but the value will be a reference to the <code>dateBean</code> bean from the application context.
This may be useful when using <emphasis>Property Placeholder Configuration</emphasis> or command line arguments.
</para>
<para>
If you need more control over how variables are generated, you can implement your own Java class
@@ -115,7 +140,7 @@
provide an implementation of <classname>ScriptVariableGenerator</classname> and reference it with the <code>script-variable-generator</code>
attribute:
<programlisting language="xml"><![CDATA[<int-script:script location="foo/bar/MyScript.groovy"
script-variable-generator="variableGenerator"/>
script-variable-generator="variableGenerator"/>
<bean id="variableGenerator" class="foo.bar.MyScriptVariableGenerator"/>]]></programlisting>
If a <code>script-variable-generator</code> is not provided, script components use
@@ -124,7 +149,7 @@
variables from the <code>Message</code> in its <code>generateScriptVariables(Message)</code> method.
<important>
You cannot provide both the <code>script-variable-generator</code> attribute and <code>&lt;variable&gt;</code> sub-element(s)
as they are mutually exclusive. Also, custom variable bindings cannot be used with an inline script.
as they are mutually exclusive.
</important>
</para>
</section>

View File

@@ -667,5 +667,13 @@
<xref linkend="file-reading"/>, <xref linkend="ftp-inbound"/>, and <xref linkend="sftp-inbound"/> for more information.
</para>
</section>
<section id="3.0-scripting-variables">
<title>Scripting Support: Variables Changes</title>
<para>
A new <code>variables</code> attribute has been introduced for scripting components.
In addition, variable bindings are now allowed for inline scripts.
See <xref linkend="groovy"/> and <xref linkend="scripting"/> for more information.
</para>
</section>
</section>
</chapter>