Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 18 Spring and Groovy So far, your Spring application has been dealing with XML files in order to help the Spring container create and instantiate your classes. In this chapter, you are going to change that by using Groovy to see how the new Spring Framework 4 plays nicely with it. Grails, a web framework that uses Groovy as its primary language, is based on the Spring Framework, making it robust, extensible, and very easy to use. Because Spring is the foundation of this framework, the Grails team did a great job introducing a new Spring DSL (domain-specific language) based entirely on Groovy. One of the most powerful features of the Groovy programming language is the ability to create a DSL easily and with minimal effort. So it was time to bring that powerful feature into the Spring Framework. The Spring team developed a new way of exposing beans through a Groovy DSL and incorporating it in the new Spring 4 release through several classes, such as the GenericGroovyApplicationContext that can accept a Groovy script defining the beans in a DSL. In this chapter, you are going to translate the beans definitions in your Spring application from XML to the new Spring DSL. Let’s Code Groovy You are going to start by creating all the Spring DSL files that will be translated from the XML configuration into the new Spring DSL in Groovy. See Listing 18-1 for the XML that you are going to translate. Listing 18-1. mydocuments-context.xml 253

Chapter 18 ■ Spring and Groovy Listing 18-1 shows the XML that you have been using in the past chapters. Now it’s time to make it more Groovy! Let's take a look at Listing 18-2, and don’t worry too much about mastering it now. I will explain in detail what you doing are and the syntax. Listing 18-2. mydocuments.groovy beans { xmlns context:"http://www.springframework.org/schema/context" context.'component-scan'('base-package': "com.apress.isf.spring") importBeans("classpath:META-INF/groovy/mydocumentsJDBC.groovy") importBeans("classpath:META-INF/groovy/mydocumentsRabbitMQ.groovy") importBeans("classpath:META-INF/groovy/mydocumentsMongo.groovy") } Listing 18-2 shows the new Spring DSL (which is equivalent to Listing 18-1). You are removing all of the clutter from the XML! Very Groovy! Next, let me explain what every part means. You need to start with a beans keyword, followed by curly braces that will contain all of the bean definitions; this will be the equivalent of starting your tag. beans { } //your beans definitions here Next, you are going to use some of the namespaces. You are going to use xmlns context:http://www.springframework.org/schema/context This namespace will be the first element after the curly brace for beans. This is equivalent to adding the first part in the XML. Next, you use the tag to tell the Spring container to look for some annotations and classes beginning in the com.apress.isf.spring Java package. Also, you are importing other beans with the method importBeans. ■ ■Note The following listings show you only the Spring DSL. These listings are equivalent to the XML. So, if needed, you can review the book’s companion source code to see the XML in detail and compare it with the new Spring DSL. Next, Listing 18-3 shows the JDBC groovy DSL, the mydocumentsJDBC.groovy file. 254

Chapter 18<br />

<strong>Spring</strong> and Groovy<br />

So far, your <strong>Spring</strong> application has been dealing with XML files in order to help the <strong>Spring</strong> container create and<br />

instantiate your classes. In this chapter, you are going to change that by using Groovy to see how the new <strong>Spring</strong><br />

<strong>Framework</strong> 4 plays nicely with it.<br />

Grails, a web framework that uses Groovy as its primary language, is based on the <strong>Spring</strong> <strong>Framework</strong>, making<br />

it robust, extensible, and very easy to use. Because <strong>Spring</strong> is the foundation of this framework, the Grails team did a<br />

great job introducing a new <strong>Spring</strong> DSL (domain-specific language) based entirely on Groovy.<br />

One of the most powerful features of the Groovy programming language is the ability to create a DSL easily and<br />

with minimal effort. So it was time to bring that powerful feature into the <strong>Spring</strong> <strong>Framework</strong>.<br />

The <strong>Spring</strong> team developed a new way of exposing beans through a Groovy DSL and incorporating it in the new<br />

<strong>Spring</strong> 4 release through several classes, such as the GenericGroovyApplicationContext that can accept a Groovy<br />

script defining the beans in a DSL.<br />

In this chapter, you are going to translate the beans definitions in your <strong>Spring</strong> application from XML to the new<br />

<strong>Spring</strong> DSL.<br />

Let’s Code Groovy<br />

You are going to start by creating all the <strong>Spring</strong> DSL files that will be translated from the XML configuration into the<br />

new <strong>Spring</strong> DSL in Groovy. See Listing 18-1 for the XML that you are going to translate.<br />

Listing 18-1. mydocuments-context.xml<br />

<br />

<br />

253

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!