3) Inlining Groovy objects
3.1) Introduction
It is also possible to inline Groovy code directly in the Spring's configuration file rather than having the code in a separate .groovy file. This will be useful at times when the size of the Groovy code is comparatively smaller.
3.2) Spring's Configuration file
inline.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">
<lang:groovy id="person">
<lang:inline-script>
package javabeat.net.articles.spring.groovy.integration.inline
public class Person
{
String name
int age
double salary
public void toString(){
println (name + "::" + age + "::" + salary);
}
}
</lang:inline-script>
<lang:property name="name" value="Nicolas" />
<lang:property name="age" value="43" />
<lang:property name="salary" value="56789" />
</lang:groovy>
</beans>
In the above configuration file, the new Groovy type called Person is created within a well-defined package. Note that the Groovy code should be embedded with the tags lang:inline. It is also possible to define an object of type Person as well as to pass parameters to the defined object with the help of the 'property' tags.
|