Webcam Chat QuickBooks Advice international calling cards international phone cards
JavaBeat Certifications Certifications Kits Articles Tutorials Tips QNA Book Store Interview Questions SCJP 1.5 SCJP 1.6 SCWCD 5.0 SCBCD 5.0 SCEA SCJA Feeds
Kaspersky Anti-Virus 2011
Submit Your Blog Feedback Request Article Print Email

Integrating Spring and Groovy

Author : Christy
Topic : spring groovy 
Pages :
Hibernate Books | Spring Books | JSF Books | Java Books

4) Reloading Groovy code

4.1) Introduction

This is a handy feature available in Spring-Groovy Integration. It is basically reloading a Groovy file to get the updated information. Note that when a Groovy file is compiled and made to run, any changes made to the Groovy source file wont affect the running code. This is the default behavior. However, this can be overridden by setting a property in the configuration file. This is illustrated with the help of the following sample,

4.2) The Person interface

Person.java


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

public interface Person {

    public String getName();
    public void setName(String name);
    
}

We have declared a Person interface for manipulating the name of a person through a getter and a setter.

4.3) PersonImpl class

The following code provides a simple implementation in Groovy for the above Person class.

PersonImpl.java


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

public class PersonImpl implements Person
{
    String name;

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }
}

4.4) Person Wrapper class

This is just a wrapper for the above Person class. Since a defined Groovy object in the Spring's configuration file cannot be directly referenced in the Client Application, this java class is introduced. This Java class will basically wrap the underlying Groovy object.

PersonWrapper.java


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

public class PersonWrapper {

    private Person person;

    public void setPerson(Person person)
    {
        this.person = person;
    }   

    public Person getPerson()
    {   
        return person;
    }
}

4.5) Configuration file

refresh.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="personImpl" 
        script-source="classpath:javabeat/net/articles/spring/groovy/integration/refresh/PersonImpl.groovy"
        refresh-check-delay="5000">
        <lang:property name = "name" value = "John"/>
    </lang:groovy>

    <bean id = "personWrapper" 
        class = "javabeat.net.articles.spring.groovy.integration.refresh.PersonWrapper">
        <property name = "person" ref = "personImpl"/>
    </bean>
    
</beans>

It is seen that we have defined a Groovy object called 'personImpl' for the type PersonImpl.groovy. We find the introduction of the new property 'refresh-check-delay', which tells that for every 5 seconds check the Groovy source file (PersonImpl.groovy in our case) and if it updated, compile the file to take the updated information. If the updated source contains syntax errors, then a Run-time Exception will be thrown.

4.6) Client Application

RefreshTest.java


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

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

public class RefreshTest {

    public static void main(String args[]) throws Exception{

        ApplicationContext context = new FileSystemXmlApplicationContext(
            "./src/resources/spring/groovy/refresh.xml");
        PersonWrapper wrapperForPerson = (PersonWrapper)context.getBean(
            "personWrapper");

        System.out.println("Old Name");
        System.out.println(wrapperForPerson.getPerson().getName());

        Thread.sleep(10 * 1000);

        System.out.println("Updated Name");
        System.out.println(wrapperForPerson.getPerson().getName());
    }
}

The Client Application makes a reference to the Person Wrapper object which wraps the underlying Groovy object (PersonImpl). When the program runs, it initially prints the name of the person as 'John' (that is how we have configured in the Xml file). Later the execution is paused for 10 seconds, which gives enough time to modify the Groovy source file. Change the old name John to something like 'Johny' to see the updated output.

Recommended Books

Submit Your Blog Feedback Request Article Print Email

Java / J2EE Tutorials

Spring Framework

Hibernate Framework

JSF Framework

Struts Framework

Java Server Pages(JSP)

Servlets

Java / J2EE Design Patterns

SCJP

SCEA


JavaBeat Website (2004-2009), India
javabeat | advertise | about us | useful resources
Copyright (2004 - 2009), JavaBeat