Use Groovy has Spring bean in a JSF web application

Updated: 2009-04-01

Using Groovy as a Spring Bean in a JSF Web Application

Using Groovy in your Java web application with the Spring Framework is both easy and powerful. This tutorial will guide you through the process.

Background

In my last project, I needed to parse a complex XML file and transfer the information to a database. Initially, I used a Java class with org.w3c.dom.* and DocumentBuilder, but the resulting code was long, complex, and difficult to maintain.

Example

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 
doc = docBuilder.parse(fileName); 
doc.getDocumentElement().normalize(); 
NodeList listOfEntries = doc.getElementsByTagName("entry"); 
for (int s = 0; s < listOfEntries.getLength(); s++) { 
  Node firstEntityNode = listOfEntries.item(s); 
  if (firstEntityNode.getNodeType() == Node.ELEMENT_NODE) { 
    ListEntry entry = new ListEntry(); 
    Element firstEntityElement = (Element) firstEntityNode; 
    NodeList uidList = firstEntityElement.getElementsByTagName("uid"); 
    ... 
  } 
} 

To simplify the process, I integrated Groovy into my Spring-JSF application. Groovy’s XmlSlurper class allows for easy XML parsing, treating XML as a collection of objects.

A useful tutorial on XmlSlurper can be found here.

Groovy’s Role in the Application

The first Groovy class in my application had the following responsibilities:

  • Accept two parameters from a Java class (XML file address and an integer parameter)
  • Process the XML file
  • Use two existing Java classes
  • Return an ArrayList of custom objects containing parsed values

Steps to Integrate Groovy with Spring

1. Add the Groovy JAR to Your Application

Ensure that your application includes the Groovy library.

2. Create a Traditional Java Interface

package ch.genidea.checknames.importer; 
 
import java.util.List; 
import ch.genidea.checknames.model.ListEntry; 
import ch.genidea.checknames.model.SourceList; 
 
public interface Parser { 
    List<ListEntry> parse(); 
    void setFilename(String filename); 
    void setSourceList(SourceList sourceList); 
} 

We declare setter methods for the two required variables that will be used in the Groovy class.

3. Create the Groovy Class in the Classpath

If using Maven, place it in src/main/resources.

package ch.genidea.checknames.importer 
 
import java.util.List; 
import ch.genidea.checknames.model.ListEntry; 
import ch.genidea.checknames.model.SourceList; 
 
def class ParserImpl implements ch.genidea.checknames.importer.Parser { 
    String filename 
    List<ListEntry> result 
    SourceList sourceList 
 
    List<ListEntry> parse() { 
        def pers = new XmlSlurper().parse(new File(filename)) 
        List<ListEntry> result = new ArrayList<>() 
         
        pers.Entry.each { 
            result.add(addEntry(it)) 
            it.akaList.aka.each { result.add(addEntry(it)) } 
        } 
        return result 
    } 
 
    ListEntry addEntry(def it) { 
        ListEntry entity = new ListEntry() 
        entity.uid = (it.uid.text() as Integer) 
        entity.sourceList = sourceList 
        entity.familyName = it.lastName.text() 
        entity.firstName = it.firstName.text() 
        return entity 
    } 
 
    void setFilename(String filename) { this.filename = filename } 
    void setSourceList(SourceList sourceList) { this.sourceList = sourceList } 
} 

With just a few lines, this Groovy class performs the same work as the original lengthy Java implementation.

4. Declare the Groovy Bean in Spring

<beans xmlns="https://www.springframework.org/schema/beans" 
       xmlns:lang="https://www.springframework.org/schema/lang" 
       ...> 
 
    <lang:groovy id="parser" script-source="classpath:ch/genidea/checknames/importer/ParserImpl.groovy" /> 
 
    <bean name="listImportService" class="ch.genidea.checknames.lists.service.ListImportServiceImpl"> 
        <property name="parser" ref="parser" /> 
    </bean> 
</beans> 

5. Use the Groovy Class in Your Code

parser.setFilename(fileSource); 
parser.setSourceList(sl); 
List<ListEntry> list = parser.parse(); 

Drawbacks

While integrating Groovy brought significant advantages, I encountered a couple of minor drawbacks:

  • Debugging Groovy in Eclipse is not straightforward. However, I used groovyConsole to quickly test changes.
  • The Groovy JAR (groovy-all.jar) added approximately 4MB to the project.

Despite these small issues, the benefits of cleaner, more readable, and maintainable code made it well worth the effort! 😊


WebApp built by Marco using Java 21 - Hosted in Switzerland