JavaBeat
Search JavaBeat

JAVABEAT
home
articles
tips
QnA
Books
forums
ARTICLE TOPICS
All Articles
Java 5.0
Java 6.0
EJB 3.0
JCA
Struts
JSF
Spring
Groovy
JBoss Seam
Hibernate
Eclipse
JavaFx
Google Guice
J2ME
GWT
WebServices
AJAX
ARCHIVE
2007 | 12 11 10 09 08 07 06 05 04 03
2008 | 07 06 05 04 03 02 01
CERTIFICATION KITS
350 SCJP 1.5 Mock Exams
400 SCJP 1.6 Mock Exams
300 SCWCD 5.0 Mock Exams
300 SCBCD 5.0 Mock Exams
Enter email address:

Latest JavaBeat Articles Delivered by FeedBurner
OUR NETWORK
javabeat
planetoss
Sponsors
Java Jobs eCommerce software Get Ubuntu 8.04 Get FireFox 3.0  

Pages : 1 2 3 4

Integrating Spring and Groovy

Author : Christy
Date : Sat Oct 20th, 2007
Topic : spring groovy 
Enter email address:

Latest JavaBeat Articles Delivered

1) Introduction

Spring makes it easier to provide integration support for scripting languages. The currently supported scripting languages are Groovy, JRuby and BeanShell. However, this article concentrates only on Spring's support for the Groovy language. Plenty of code samples are given in each section for much clarity. The article assumes that the reader is comfortable in both Spring and Groovy concepts. If you are new to Spring and Groovy frameworks, please read the introduction articles that have been published in javabeat for Spring Introduction to Spring Web Framework and Groovy Introduction to Groovy - Scripting Language before reading this article.

2) Groovy Objects in Spring's Environment

2.1) Introduction

Let us see a sample Application that will illustrate the integration of Groovy in Spring's Environment. It will discuss how a Groovy file (the file with extension as '.groovy') can be integrated with Spring to take the power of a dynamic language. In this sample, a Java Application tries to access Groovy code with the help of the configuration information provided by Spring.

2.2) Server interface

Let us define a common interface called Server which is to be given implementation by both Java and Groovy. This interface has three methods start(), restart() and stop(). The following code illustrates for the same.

Server.java


package javabeat.net.articles.spring.groovy.integration.simple;

public interface Server {

    public void start();
    public void restart();
    public void stop();

}

2.3) Groovy Server

The following code presents the Groovy implementation for the above Server interface. The syntax of Groovy resembles almost the same as the Java language. However, Groovy enjoys the benefits of a scripting language like loose type checking, lenient syntax etc.. For example, it is not necessary to suffix a statement terminator with a colon (:) in Groovy.

GroovyServer.java


package javabeat.net.articles.spring.groovy.integration.simple

public class GroovyServer implements Server
{
    public void start()
    {
        println ("Groovy Server started...")
    }

    public void restart()
    {
        println ("Groovy Server restarted...")
    }

    public void stop()
    {
        println ("Groovy Server stopped...")
    }
}

2.4) Java Server

Next comes the implementation in the Java language for the Server interface. The implementation class JavaServer contains a reference with name 'groovyServer' to the Server interface. Later on, through the configuration file, we will see that how we populate this 'groovyServer' reference with the implementation given in Groovy. We also have added a test method by name 'testGroovyServer' that will call the Groovy implementation.

JavaServer.java


package javabeat.net.articles.spring.groovy.integration.simple;

public class JavaServer implements Server{

    private Server groovyServer;

    public JavaServer() {
    }

    public void start() {
        System.out.println("Java server started....");
    }

    public void restart() {
        System.out.println("Java server re-started....");
    }

    public void stop() {
        System.out.println("Java server stopped....");
    }   

    public void setGroovyServer(Server groovyServer)
    {
        this.groovyServer = groovyServer;
    }

    public void testGroovyServer(){
        groovyServer.start();
        groovyServer.restart();
        groovyServer.stop();
    }
}

2.5) Configuration file

spring-groovy.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="groovyServer" 
        script-source="classpath:javabeat/net/articles/spring/groovy/integration/simple/GroovyServer.groovy">
    </lang:groovy>

    <bean id="javaServer" 
        class="javabeat.net.articles.spring.groovy.integration.simple.JavaServer">
        <property name = "groovyServer" ref = "groovyServer" />
    </bean>    
               
</beans>

In the above configuration file, we have added a new element called 'groovy' taken from the xml name-space 'lang' to provide Groovy support. The value for the 'script-source' attribute says that look into the classpath of the application (classpath: ) for a file by name GroovyServer.groovy within the directory (javabeat/net/articles/spring/groovy/integration/simple). By this, we have imported the Groovy code into the Spring Environment and now we have a Groovy bean identified by 'groovyServer'.

In this Xml configuration file, we have also defined a Spring bean called 'javaServer' by making a reference to the previously declared Groovy Bean 'groovyServer'. So now the Java Code can access Groovy Server through the Groovy reference.

2.6) Client Application

The following client application tries to access the configuration file using the Spring Api to get a reference to the javaServer object. Then the javaServer object accesses the Groovy code by calling the method testGroovy() within which an instance of Groovy Server object is already populated and thereby the Groovy code gets executed.

SpringGroovyIntegrationTest.java


package javabeat.net.articles.spring.groovy.integration.simple;

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

public class SpringGroovyIntegrationTest {

    public static void main(String args[]){

        ApplicationContext context = new FileSystemXmlApplicationContext(
            "./src/resources/spring/groovy/spring-groovy.xml");

        JavaServer javaServer = (JavaServer)context.getBean("javaServer");
        javaServer.start();
        javaServer.restart();
        javaServer.stop();

        javaServer.testGroovyServer();
    }
}

 
Pages : 1 2 3 4
 

Sponsors
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Media (2004-2008), India
javabeat | planetoss | links directory | advertise
Copyright (2004 - 2008), JavaBeat