ICE09 . playing with java, scala, groovy and spring .

Playing with Spring

Groovy Spring

Posted by ice09 on June 6, 2008

Though the not really persuading (Open Source) IDE support, Groovy is my favorite script language for the Java VM. To have at least simplest support for Groovy, the Eclipse Plugin can be used. However, do not expect too much of it – it seems to be quite unstable (there are some suspicious OOME under Linux), but the Syntax Highlighting is ok. It is said that IntelliJ has the best Groovy IDE support by far, but I didn’t test it.
There exists nice Spring support for Groovy – you can transparently use Java- and Groovybeans. Combining this with Maven makes it really easy to make Spring even groovier.

Preparation

Setup

This post will focus on the Groovy usage within Spring, therefore the use case is quite simple.

  • A central Java bean has a list of several beans (ie. resembles a workflow)
  • The injected beans must implement an interface ITestStep which is very generic (Object execute(Object))
  • Implementing an interface seems the only way to inject Groovybeans transparently

Code

The Maven POM:

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>maven.groovy</groupId>
  <artifactId>maven.groovy</artifactId>
  <name>maven.groovy</name>
  <version>0.0.1-SNAPSHOT</version>
  <description/>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring</artifactId>
      <version>2.5.1</version>
    </dependency>
    <dependency>
      <groupId>groovy</groupId>
      <artifactId>groovy</artifactId>
      <version>1.1-rc-1</version>
    </dependency>
  </dependencies>
</project>

The Spring config:

<?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="groovyTestStep"
		script-source="classpath:SimpleGroovyClass.groovy">
	</lang:groovy>

	<bean id="testFlow" class="de.groovysample.JavaGroovyTest">
<property name="testSteps">
	<list>
				<ref bean="groovyTestStep" />
			</list>
		</property>
	</bean>

</beans>

The Beans (ie. workflowsteps) interface:

package de.groovysample;

public interface ITestStep {

	Object execute(Object arg);

}

The Groovybean (NOTE: this gose to src/main/resources as SimpleGroovyClass.groovy):

import de.groovysample.ITestStep

class SimpleGroovyClass implements ITestStep {

	Object execute(Object arg) {
		println "groovy called with arg: $arg"
	}

}

The Workflow Javaclass the Groovybean is injected to:

package de.groovysample;

import java.util.ArrayList;
import java.util.List;

public class JavaGroovyTest {

	private List<ITestStep> testSteps = new ArrayList<ITestStep>();

	public void setTestSteps(List<ITestStep> testSteps) {
		this.testSteps = testSteps;
	}

	public Object execute(Object arg) {
		for (ITestStep testStep : testSteps) {
			testStep.execute(arg);
		}
		return null;
	}

}

and the Main class:

package de.groovysample;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
		JavaGroovyTest test = (JavaGroovyTest) app.getBean("testFlow");
		System.out.println(test.execute("called from Java"));
	}

}

Resulting in the overall project setup

That’s all, this way you can easily combine Java and Groovy beans (which must implement an interface).

Advertisements

One Response to “Groovy Spring”

  1. […] Create a quickstart Maven project (using the Eclipse-Maven plugin, compare this post). […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: