Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 197 and 198: Chapter 14 ■ Using Dynamic Langua
- Page 199 and 200: Chapter 14 ■ Using Dynamic Langua
- Page 201 and 202: Chapter 15 ■ Spring Data Within Y
- Page 203 and 204: Chapter 15 ■ Spring Data Within Y
- Page 205 and 206: Chapter 15 ■ Spring Data Within Y
- Page 207 and 208: Chapter 15 ■ Spring Data Within Y
- Page 209 and 210: Chapter 15 ■ Spring Data Within Y
- Page 211 and 212: Chapter 15 ■ Spring Data Within Y
- Page 213 and 214: Chapter 15 ■ Spring Data Within Y
- Page 215 and 216: Chapter 16 ■ Messaging with Your
- Page 217 and 218: Chapter 16 ■ Messaging with Your
- Page 219 and 220: Chapter 16 ■ Messaging with Your
- Page 221 and 222: Chapter 16 ■ Messaging with Your
- Page 223 and 224: Chapter 16 ■ Messaging with Your
- Page 225 and 226: Chapter 16 ■ Messaging with Your
- Page 227 and 228: Chapter 17 Be Social and Go Mobile
- Page 229 and 230: Chapter 17 ■ Be Social and Go Mob
- Page 231 and 232: Chapter 17 ■ Be Social and Go Mob
- Page 233 and 234: Chapter 17 ■ Be Social and Go Mob
- Page 235 and 236: Chapter 17 ■ Be Social and Go Mob
- Page 237 and 238: Chapter 17 ■ Be Social and Go Mob
- Page 239 and 240: Chapter 17 ■ Be Social and Go Mob
- Page 241 and 242: Chapter 17 ■ Be Social and Go Mob
- Page 243 and 244: Chapter 17 ■ Be Social and Go Mob
- Page 245 and 246: Chapter 17 ■ Be Social and Go Mob
- Page 247: Part IV Spring I/O Spring I/O is no
- Page 251 and 252: Chapter 18 ■ Spring and Groovy Li
- Page 253 and 254: Chapter 18 ■ Spring and Groovy cl
- Page 255 and 256: Chapter 18 ■ Spring and Groovy If
- Page 257 and 258: Chapter 18 ■ Spring and Groovy Li
- Page 259 and 260: Chapter 19 ■ Spring Boot, Simplif
- Page 261 and 262: Chapter 19 ■ Spring Boot, Simplif
- Page 263 and 264: Chapter 19 ■ Spring Boot, Simplif
- Page 265 and 266: Chapter 19 ■ Spring Boot, Simplif
- Page 267 and 268: Chapter 19 ■ Spring Boot, Simplif
- Page 269 and 270: Chapter 19 ■ Spring Boot, Simplif
- Page 271 and 272: Chapter 19 ■ Spring Boot, Simplif
- Page 273 and 274: Chapter 20 ■ Your First Spring XD
- Page 275 and 276: Chapter 20 ■ Your First Spring XD
- Page 277 and 278: Chapter 20 ■ Your First Spring XD
- Page 279 and 280: Chapter 20 ■ Your First Spring XD
- Page 281 and 282: Chapter 20 ■ Your First Spring XD
- Page 283 and 284: Appendix A Installing Tools In this
- Page 285 and 286: Appendix A ■ Installing Tools Fig
- Page 287 and 288: Setting Environment Variables Appen
- Page 289 and 290: Appendix A ■ Installing Tools Fig
- Page 291 and 292: Appendix A ■ Installing Tools 6.
- Page 293 and 294: Appendix A ■ Installing Tools Fig
- Page 295 and 296: Appendix A ■ Installing Tools ■
- Page 297 and 298: Appendix A ■ Installing Tools Fig
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