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

Listing 18-4 shows the xmlns mongo namespace and its equivalent from the XML tag converted into<br />

the DSL and how to use it to create your beans. Another cool feature of the Groovy programming language and the<br />

<strong>Spring</strong> DSL is that you can import classes and give them an alias, like the MongoTemplate class with alias MT. This way<br />

you can avoid putting in the whole class.<br />

Next, let’s see the OXM that you used in Chapter 16 to convert from XML to object and vice versa. See Listing 18-5<br />

for the mydocumentsOXM.groovy file.<br />

Listing 18-5. mydocumentsOXM.groovy<br />

import org.springframework.amqp.support.converter.MarshallingMessageConverter as MMC<br />

import org.springframework.oxm.xstream.XStreamMarshaller as XM<br />

import com.apress.isf.java.model.Document<br />

beans{<br />

}<br />

messageConverter(MMC,ref("xstreamMarshaller"))<br />

xstreamMarshaller(XM){<br />

aliases = [document:Document]<br />

mode = 1001<br />

}<br />

Listing 18-5 shows the OXM with the XStream Marshaller and the use of an alias for the imports. Also, it shows<br />

the import and alias feature that helps you avoid putting too much code in the definitions. Next, let’s see Listing 18-6<br />

for the RabbitMQ bean definitions.<br />

Listing 18-6. mydocumentsRabbitMQ.groovy<br />

import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter as MLA<br />

beans{<br />

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

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

rabbit.'connection-factory'(id:"rabbitConnectionFactory", host:"localhost")<br />

rabbit.admin('connection-factory':'rabbitConnectionFactory')<br />

rabbit.template(id:"rabbitTemplate",<br />

'connection-factory':"rabbitConnectionFactory",<br />

'message-converter':"messageConverter")<br />

rabbit.'direct-exchange'(name:"mydocuments"){<br />

rabbit.bindings{<br />

rabbit.binding(key:".pdf", queue:"docs-pdf")<br />

rabbit.binding(key:".txt", queue:"docs-txt")<br />

rabbit.binding(key:".url", queue:"docs-web")<br />

}<br />

}<br />

256

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

Saved successfully!

Ooh no, something went wrong!