Spring and Java
The Spring Framework is a leading full-stack Java/J2EE application framework. It is aimed primarily at Java projects and delivers significant benefits such as reducing development effort and costs while providing facilities to improve test coverage and quality. It also has mechanisms for allowing beans to be backed by dynamic language code including Groovy and other languages.
Let's have a look at Spring in action for a Java application which how to use Spring's support for writing dynamic language backed beans to extend an existing Java application. The existing Java application prints out information about a country.countries in sorted order. We want to be able to add new countries and new sorting algorithms but using different languages for each one. Our goal is to not have to change the original Java code but add the new features just through the application wiring.
Suppose we have the following interface:
...
| Code Block | ||
|---|---|---|
| ||
USA Info: USA[Capital=Washington, D.C., Population=298444215] |
Spring and Groovy
We can extend this example and introduce Groovy in a number of ways. Firstly, we can create the following Groovy class:
...
In these examples, the <property/> and <lang:property/> elements allows us to use setter-based injection.
Spring and Ruby
If we prefer to code in another language (with a few restrictions - see below), Spring supports other languages too, e.g.:
...
| Code Block | ||
|---|---|---|
| ||
...
<lang:jruby id="country4" script-interfaces="spring.Country" script-source="classpath:spring/Fiji.rb">
<lang:property name="capital" value="Suva" />
<lang:property name="population" value="905949" />
</lang:jruby>
...
|
Spring and Groovy Again
But wait there's more ...
...
| Code Block | ||
|---|---|---|
| ||
...
<lang:groovy id="sorter">
<lang:inline-script><![CDATA[
package spring
class CountrySorter implements Sorter {
String order
List sort(Country[] items) {
List result = items.toList().sort{ p1, p2 -> p1.population <=> p2.population }
if (order == "reverse") return result.reverse() else return result
}
}
]]></lang:inline-script>
<lang:property name="order" value="forward" />
</lang:groovy>
...
|
Putting it all together
We now combine all of the approaches above in a final example.
...
| Code Block | ||
|---|---|---|
| ||
Unsorted: [Australia[Capital=Canberra, Population=20264082], USA[Capital=Washington, D.C., Population=298444215], NewZealand[Capital=Wellington, Population=4076140], JRuby object [Fiji[Capital=Suva, Population=905949]]] Sorted: [JRuby object [Fiji[Capital=Suva, Population=905949]], NewZealand[Capital=Wellington, Population=4076140], Australia[Capital=Canberra, Population=20264082], USA[Capital=Washington, D.C., Population=298444215]] |
What we didn't tell you yet
- Spring supports BeanShell in addition to JRuby and Groovy
- Spring supports the concept of refreshable beans when using the
<lang:language/>element so that if your bean source code changes, the bean will be reloaded (see GINA or the Spring doco for more details) - The Groovy scripting examples in the current Spring documentation are based on an old version of Groovy, ignore the
@Propertykeywords and use the latestgroovy-all-xxx.jarinstead of the jars they recommend - The
<lang:language/>element currently only supports setter-based injection - Spring automatically converts between the object models of the various languages but there are some limitations (particularly with JRuby - see the next section)
Current Limitations
Currently using the Groovy language through Spring is extensively supported. Using other languages like JRuby takes a little more care. Try restricting your JRuby methods to ones which take and return simple data types, e.g. long and String. You may also find that certain operations don't work as you'd expect when working between Ruby and other languages, e.g. if you defined a compareTo method in our Fiji.rb file, it would return long by default rather than the int which Java is expecting. In addition, the compareTo method takes an other object as a parameter. Currently the wrapping of this other object from Java or Groovy into a Ruby object hides the original Java methods.
Further Information
- Section 11.5 of GINA
- Spring documentation for scripting
- The Spring example in Groovy and JMX