Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 8 ■ Give Advice to Your Spring Application 2014-02-23 20:29:30,241 DEBUG [main] @@@@(BEFORE) Method called: listAll 2014-02-23 20:29:30,241 DEBUG [main] @@@@(BEFORE) No arguments passed. 2014-02-23 20:29:30,241 DEBUG [main] @@@@(BEFORE) Method called: findByLocation 2014-02-23 20:29:30,241 DEBUG [main] @@@@(BEFORE) Argument passed:/some/path/ 2014-02-23 20:29:30,241 ERROR [main] findByLocation not yet implemented. It’s magic! Yes! You are now advising your code, removing the entire scattering and tangling of the logs. Your SearchEngine implementation is now clean and you can just focus on your business logic. After Advice The after advice will be after the method call, and you can get a hold of the return value. This advice is done by implementing the AfterReturningAdvice interface class. See Listing 8-8 for an example. Listing 8-8. AfterLoggingModule.java package com.apress.isf.spring.aop; import java.lang.reflect.Method; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.aop.AfterReturningAdvice; public class AfterLoggingModule implements AfterReturningAdvice { private static final Logger log = LoggerFactory.getLogger(AfterLoggingModule.class); } public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { if(log.isDebugEnabled()){ log.debug("@@@@(AFTER) Method called: " + method.getName()); if(args.length ==0 ) log.debug("@@@@(AFTER) No arguments passed."); for(Object arg:args) log.debug("@@@@(AFTER) Argument passed:" + arg); log.debug("@@@(AFTER) Result: " + returnValue); } } Listing 8-8 implements the method afterReturning that will have the returned value, the method that was intercepted, the arguments of the method, and the object where the method was intercepted. Now let’s modify your XML. Remember it will almost be the same as Listing 8-7, but now you are going to change some ids, as shown in Listing 8-9. 98

Chapter 8 ■ Give Advice to Your Spring Application Listing 8-9. mydocuments-aop-context.xml afterLogging Running the unit test (see Listing 8-5) with gradle :ch08:test will have the following output: 2014-02-23 20:39:40,683 DEBUG [main] Using Spring AOP: 2014-02-23 20:39:40,747 DEBUG [main] @@@@(AFTER) Method called: findByType 2014-02-23 20:39:40,748 DEBUG [main] @@@@(AFTER) Argument passed:Type(name: WEB, description: Web Link, extension: .url) 2014-02-23 20:39:40,748 DEBUG [main] @@@(AFTER) Result: [Documents(name: Pro Spring Security Book, type: Type(name: WEB, description: Web Link, extension: .url), location: http://www.apress.com/9781430248187)] 2014-02-23 20:39:40,748 DEBUG [main] @@@@(AFTER) Method called: listAll 2014-02-23 20:39:40,748 DEBUG [main] @@@@(AFTER) No arguments passed. 2014-02-23 20:39:40,748 DEBUG [main] @@@(AFTER) Result: [Documents(name: Book Template, type: Type(name: PDF, description: Portable Document Format, extension: .pdf), location: /Users/ felipeg/Documents/Random/Book Template.pdf), Documents(name: Sample Contract, type: Type(name: PDF, description: Portable Document Format, extension: .pdf), location: /Users/felipeg/Documents/ Contracts/Sample Contract.pdf), Documents(name: Clustering with RabbitMQ, type: Type(name: NOTE, 99

Chapter 8 ■ Give Advice to Your <strong>Spring</strong> Application<br />

Listing 8-9. mydocuments-aop-context.xml<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

afterLogging<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Running the unit test (see Listing 8-5) with<br />

gradle :ch08:test<br />

will have the following output:<br />

2014-02-23 20:39:40,683 DEBUG [main] Using <strong>Spring</strong> AOP:<br />

2014-02-23 20:39:40,747 DEBUG [main] @@@@(AFTER) Method called: findByType<br />

2014-02-23 20:39:40,748 DEBUG [main] @@@@(AFTER) Argument passed:Type(name: WEB, description: Web<br />

Link, extension: .url)<br />

2014-02-23 20:39:40,748 DEBUG [main] @@@(AFTER) Result: [Documents(name: Pro <strong>Spring</strong> Security Book,<br />

type: Type(name: WEB, description: Web Link, extension: .url), location:<br />

http://www.apress.com/9781430248187)]<br />

2014-02-23 20:39:40,748 DEBUG [main] @@@@(AFTER) Method called: listAll<br />

2014-02-23 20:39:40,748 DEBUG [main] @@@@(AFTER) No arguments passed.<br />

2014-02-23 20:39:40,748 DEBUG [main] @@@(AFTER) Result: [Documents(name: Book Template, type:<br />

Type(name: PDF, description: Portable Document Format, extension: .pdf), location: /Users/<br />

felipeg/Documents/Random/Book Template.pdf), Documents(name: Sample Contract, type: Type(name:<br />

PDF, description: Portable Document Format, extension: .pdf), location: /Users/felipeg/Documents/<br />

Contracts/Sample Contract.pdf), Documents(name: Clustering with RabbitMQ, type: Type(name: NOTE,<br />

99

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

Saved successfully!

Ooh no, something went wrong!