25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 18 ■ <strong>Spring</strong> and Groovy<br />

<br />

<br />

<br />

<br />

<br />

<br />

Listing 18-1 shows the XML that you have been using in the past chapters. Now it’s time to make it more Groovy!<br />

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<br />

doing are and the syntax.<br />

Listing 18-2. mydocuments.groovy<br />

beans {<br />

xmlns context:"http://www.springframework.org/schema/context"<br />

context.'component-scan'('base-package': "com.apress.isf.spring")<br />

importBeans("classpath:META-INF/groovy/mydocumentsJDBC.groovy")<br />

importBeans("classpath:META-INF/groovy/mydocumentsRabbitMQ.groovy")<br />

importBeans("classpath:META-INF/groovy/mydocumentsMongo.groovy")<br />

}<br />

Listing 18-2 shows the new <strong>Spring</strong> DSL (which is equivalent to Listing 18-1). You are removing all of the clutter<br />

from the XML! Very Groovy! Next, let me explain what every part means.<br />

You need to start with a beans keyword, followed by curly braces that will contain all of the bean definitions; this<br />

will be the equivalent of starting your tag.<br />

beans {<br />

}<br />

//your beans definitions here<br />

Next, you are going to use some of the namespaces. You are going to use<br />

xmlns context:http://www.springframework.org/schema/context<br />

This namespace will be the first element after the curly brace for beans. This is equivalent to adding the first part<br />

in the XML. Next, you use the tag to tell the <strong>Spring</strong> container to look for some annotations and<br />

classes beginning in the com.apress.isf.spring Java package. Also, you are importing other beans with the method<br />

importBeans.<br />

■ ■Note The following listings show you only the <strong>Spring</strong> DSL. These listings are equivalent to the XML. So, if needed,<br />

you can review the book’s companion source code to see the XML in detail and compare it with the new <strong>Spring</strong> DSL.<br />

Next, Listing 18-3 shows the JDBC groovy DSL, the mydocumentsJDBC.groovy file.<br />

254

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

Saved successfully!

Ooh no, something went wrong!