5) Customizing Groovy objects creation
5.1) Introduction
In this final section, we will see how it is possible to customize the Groovy object creation process. The solution lies in the hands of GroovyObjectCustomizer interface. All we have to do is to override the interface to provide the custom logic for the object once it is created. Let us look into that.
5.2) Test Class
We have defined a Test Class in Groovy with a single property called strProp. Whenever an object for this class is created the strProp will soon be populated with the value 'A String Value'. Let us see in the subsequent sections how to modify the original value through customization process.
TestClass.java
package javabeat.net.articles.spring.groovy.integration.customization
public class TestClass
{
String strProp = "A String Value"
}
5.3) Test Wrapper class
Since Spring doesn't provide direct support for accessing a Groovy object defined in the Configuration file, we provide a Java wrapper object here. Note that this class contains a reference to GroovyObject. All objects in Groovy are of the generic type 'GroovyObject'. So, it means that even the Groovy object of type 'TestClass' is a kind of 'GroovyObject' object.
TestClassWrapper.java
package javabeat.net.articles.spring.groovy.integration.customization;
import groovy.lang.GroovyObject;
public class TestClassWrapper {
private GroovyObject groovyObject;
public TestClassWrapper() {
}
public void setGroovyObject(GroovyObject groovyObject)
{
this.groovyObject = groovyObject;
}
public void testIt()
{
System.out.println("Str value is ---> " +
groovyObject.getProperty("strProp"));
}
}
5.4) Test Class Customizer
Now let us define the customization class for Groovy. As mentioned, the class should conform to the interface GroovyObjectCustomizer and we should over-ride the method customize() which will be called once the Groovy object is created.
TestClassCustomizer.java
package javabeat.net.articles.spring.groovy.integration.customization;
import groovy.lang.GroovyObject;
import org.springframework.scripting.groovy.GroovyObjectCustomizer;
public class TestClassCustomizer implements GroovyObjectCustomizer {
public void customize(GroovyObject groovyObject) {
groovyObject.setProperty("strProp",
"new value for string property");
}
}
In the above method, we have modified the value of the string property 'strProp' to a new value.
5.5) Configuration file
Let us see how all these classes get wired in a single Xml file. To enable customization, we have defined the customization class 'TestClassCustomizer' that we created just before by giving an identifier 'testClassCustomizer'. Note that the declaration of the Groovy object testClass contains a reference to the Test Customizer class through its 'customizer-ref' attribute.
custom.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">
<bean id="testClassCustomizer"
class="javabeat.net.articles.spring.groovy.integration.customization.TestClassCustomizer" />
<lang:groovy id="testClass"
script-source="classpath:javabeat/net/articles/spring/groovy/integration/customization/TestClass.groovy"
customizer-ref="testClassCustomizer">
</lang:groovy>
<bean id = "testClassWrapper"
class = "javabeat.net.articles.spring.groovy.integration.customization.TestClassWrapper">
<property name = "groovyObject" ref = "testClass"/>
</bean>
</beans>
5.6) The Client Application
The Client Application is quite simple. It makes a reference to the Test Wrapper class and then calls the testIt() method to ensure that the property for the groovy object is changed.
RunTestClass.java
package javabeat.net.articles.spring.groovy.integration.customization;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class RunTestClass {
public static void main(String args[]){
ApplicationContext context = new FileSystemXmlApplicationContext(
"./src/resources/spring/groovy/custom.xml");
TestClassWrapper wrapper = (TestClassWrapper)context.getBean(
"testClassWrapper");
wrapper.testIt();
}
}
6) Conclusion
In this article, we have seen how Spring provides Integration support to Groovy through code samples. The initial section of the article showed a simple Application that defines how Integration for Scripting languages are wired through the Configuration information. Then it described the inlining feature where it is possible to code the Groovy logic directly into the Configuration file. Later on, it talked about the Reloading nature of the Groovy source code. Finally the article was concluded by illustrating the Object Creation Customization process.
|