Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
package spring
public class NewZealand extends Australia implements Country {
    String toString() {
        return "NewZealand[Capital=" + capital + ", Population=" + population + "]"
    }
}

So long as the first corresponding .class file is available on the classpath in compiled form(e.g. by your IDE or by manually running groovyc), we can simply reference this class in our beans.xml file as follows. E.g. for the first class we can use:

Code Block
...
<bean id="country1" class="spring.Australia">
    <property name="capital" value="Canberra"/>
    <property name="population" value="20264082"/>
</bean>
...

Alternatively, if the source file will be is on the classpath, we can use special Spring notation to reference it, e.g. for the second class we can use:

Code Block
...
<lang:groovy id="country3" script-source="classpath:spring/NewZealand.groovy">
    <lang:property name="capital" value="Wellington" />
    <lang:property name="population" value="4076140" />
</lang:groovy>
...

...

Suppose now that we wish to sort our countries according to population size. We don't want to use Java's built-in sort mechanisms as some of them rely on our objects implementing the Comparable interface and we don't want that noise in our Ruby script. Instead we will use Groovy. We could simply write a Sort class in Groovy and reference as we have done above. This time however we are going to use an additional Spring feature and have the scripting code within our beans.xml file. First we define the following Java interface:

Code Block
java
java
package spring;

import java.util.List;

public interface Sorter {
    List sort(Country[] unsorted);
}

...