<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jason R Briggs &#187; spring</title>
	<atom:link href="http://www.briggs.net.nz/log/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.briggs.net.nz/log</link>
	<description>Techie stuff from the perspective of a Kiwi abroad</description>
	<lastBuildDate>Mon, 28 Jun 2010 06:45:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Scripting in Spring</title>
		<link>http://www.briggs.net.nz/log/2007/11/01/scripting-in-spring/</link>
		<comments>http://www.briggs.net.nz/log/2007/11/01/scripting-in-spring/#comments</comments>
		<pubDate>Thu, 01 Nov 2007 09:18:56 +0000</pubDate>
		<dc:creator>jrbriggs</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://www.briggs.net.nz/log/2007/11/01/scripting-in-spring/</guid>
		<description><![CDATA[Spring has a rather nice scripting framework that I haven&#8217;t paid much attention to, more from a lack of need than any real lack of interest. One thing I recently noticed, is that there are only 3 supported scripting languages: Beanshell, Groovy and JRuby. Now Beanshell (for myself at least) was interesting about 4 or [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.springframework.org">Spring</a> has a rather nice scripting framework that I haven&#8217;t paid much attention to, more from a lack of need than any real lack of interest.</p>
<p>One thing I recently noticed, is that there are only 3 supported scripting languages: Beanshell, Groovy and JRuby.  Now Beanshell (for myself at least) was interesting about 4 or 5 years ago (maybe longer&#8230; since I can&#8217;t quite recall when it was first released), and I have little interest in Groovy (read &#8220;little interest&#8221; as &#8220;<em>no</em> interest&#8221;).</p>
<p>A bit of googling only uncovered a brief <a href="http://forum.springframework.org/archive/index.php/t-9898.html">discussion</a> about adding Jython support back in 2004 (I haven&#8217;t been able to find any code to go with that discussion), and <a href="http://rhinoinspring.sourceforge.net/">Rhino-in-Spring</a> which, as far as I can tell, is aimed at the web tier.  Interesting that 2 of the most popular (IMO) scripting languages are rather conspicious by their absence from the core Spring distribution.  Or if not absent, then certainly not very visible.</p>
<p>I&#8217;ve been looking for a reason to hack around with Spring extensions for a while, so this seemed like the opportune moment.  Following one of the comments in that 2004 discussion, I started with cloning <a href="http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/scripting/groovy/GroovyScriptFactory.html">GroovyScriptFactory</a> and a few other classes from the scripting package, and then hacked away the bits I didn&#8217;t want or need.</p>
<p><a href="http://www.briggs.net.nz/hg/spring-script/file/tip/src/script/GenericScriptFactory.java">GenericScriptFactory</a> is my abstract base class, and contains most of the code from GroovyScriptFactory.  Subclass <a href="http://www.briggs.net.nz/hg/spring-script/file/tip/src/script/jython/JythonScriptFactory.java">JythonScriptFactory</a> is the workhorse for Jython scripting, while <a href="http://www.briggs.net.nz/hg/spring-script/file/tip/src/script/rhino/RhinoScriptFactory.java">RhinoScriptFactory</a> is obviously for Javascript code.</p>
<p>The cool part &#8212; I&#8217;ve implemented a NamespaceHandler for both, so bean configuration looks like this for Jython:</p>
<pre>
&lt;script:jython id="test1" script-source="test.py"
               bean-name="mytest" return-type="test.TestInterface"
               interpreter="pythonInterpreter"&gt;
    &lt;script:property name="test" value="jython prop from spring" /&gt;
&lt;/script:jython&gt;
</pre>
<p>and for Javascript (Rhino):</p>
<pre>
&lt;script:rhino id="test2" script-source="test.js"
              bean-name="mytest" return-type="test.TestInterface"
              scope="jsScope"&gt;
    &lt;script:property name="test" value="javascript prop from spring" /&gt;
&lt;/script:rhino&gt;
</pre>
<p>You&#8217;ll note that in both cases the attributes are passed to the factory, but properties are injected after the bean (in whatever language) has been created.  A distinction which is rather useful when you think about it.  This is the same as the other scripting languages already built-in to Spring.</p>
<p>You can see the full configuration file <a href="http://www.briggs.net.nz/hg/spring-script/file/tip/src/beans.xml">here</a>, which includes setting up a <a href="http://www.jython.org/docs/javadoc/org/python/util/PythonInterpreter.html">PythonInterpreter</a> for Jython, and a global <a href="http://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/Scriptable.html">scope</a> for Rhino.</p>
<p>The sweet spot is, of course, this configuration:</p>
<pre>
&lt;script:jython id="test3" script-source="test.py" bean-name="mytest" return-type="test.TestInterface" interpreter="pythonInterpreter"&gt;
    &lt;script:property name="testInterface" ref="test2" /&gt;
&lt;/script:jython&gt;
</pre>
<p><em>test2</em> is the object created by the Javascript code, and you&#8217;ll note that it&#8217;s being injected into the object created (in this case) by Jython code.  The opposite works as well, meaning you can call Jython code from Javascript and Javascript code from Jython (well, at least through Spring injection).  Exactly why you&#8217;d want to do this, I don&#8217;t know, but it&#8217;s a fun feature.</p>
<p>In either case, my classes implement a simple Java interface, meaning the beans created by both scripts are useable from Java as well &#8212; as you can see in the <a href="http://www.briggs.net.nz/hg/spring-script/file/tip/src/Test.java">Test</a> code:</p>
<pre>
<code>
public static final void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "beans.xml" });
    BeanFactory factory = (BeanFactory) context;

    TestInterface ti1 = (TestInterface) factory.getBean("test1");
    System.out.println("jython test: " + ti1.getTest());

    TestInterface ti2 = (TestInterface) factory.getBean("test2");
    System.out.println("rhino test: " + ti2.getTest());
}
</code>
</pre>
<p>Anyone interested can find the whole thing (very much a work in progress) in a Mercurial repository <a href="http://www.briggs.net.nz/hg/spring-script">here</a>.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.briggs.net.nz/log/2007/11/01/scripting-in-spring/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
