A quick example should suffice for defining and executing a Groovy Bean inside a Spring application context. Here's a short Java interface:
package com.springandgroovy;
public interface HelloWorldService {
String sayHello();
}
And here's the Groovy implementation:
import com.springandgroovy.HelloWorldService;
class HelloWorldServiceImpl implements HelloWorldService {
String name
String sayHello()
{
"Hello $name. Welcome to Scripting in Groovy."
}
}
The Spring application context is:
<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.5.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">
<lang:groovy id="helloWorldService"
script-source="classpath:com/springandgroovy/HelloWorldServiceImpl.groovy">
<lang:property name="name" value="meera"/>
</lang:groovy>
Finally, here's an execution example:
HelloWorldService service = (HelloWorldService) context.getBean(
"helloWorldService");
System.out.println(service.sayHello());