Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 8 ■ Give Advice to Your <strong>Spring</strong> Application<br />
2014-02-23 20:46:25,689 DEBUG [main] @@@(AROUND) Processing...<br />
2014-02-23 20:46:25,732 DEBUG [main] @@@(AROUND-AFTER) Result: [Documents(name: Pro <strong>Spring</strong><br />
Security Book, type: Type(name: WEB, description: Web Link, extension: .url), location:<br />
http://www.apress.com/9781430248187)]<br />
2014-02-23 20:46:25,732 DEBUG [main] @@@@(AROUND-BEFORE) Method called: listAll<br />
2014-02-23 20:46:25,732 DEBUG [main] @@@@(AROUND-BEFORE) No arguments passed.<br />
2014-02-23 20:46:25,732 DEBUG [main] @@@(AROUND) Processing...<br />
2014-02-23 20:46:25,732 DEBUG [main] @@@(AROUND-AFTER) Result: [Documents(name: Book Template,<br />
type: 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 />
description: Text Notes, extension: .txt), location: /Users/felipeg/Documents/Random/Clustering with<br />
RabbitMQ.txt), Documents(name: Pro <strong>Spring</strong> Security Book, type: Type(name: WEB, description: Web<br />
Link, extension: .url), location: http://www.apress.com/9781430248187)]<br />
2014-02-23 20:46:25,733 DEBUG [main] @@@@(AROUND-BEFORE) Method called: findByLocation<br />
2014-02-23 20:46:25,733 DEBUG [main] @@@@(AROUND-BEFORE) Argument passed:/some/path/<br />
2014-02-23 20:46:25,733 DEBUG [main] @@@(AROUND) Processing...<br />
2014-02-23 20:46:25,733 ERROR [main] findByLocation not yet implemented.<br />
As you can see in the output, you are intercepting a method before it’s actually called. You are logging its name<br />
and its parameters (if any), and then you are executing the object’s method and logging the result. Another way to use<br />
the around advice is to use a cache mechanism to a database or a remote site. So if there are several calls to the same<br />
method, you can cache some data in memory instead of going to a database or going to the remote site. You will see<br />
more examples in the following sections.<br />
After Throwing Advice<br />
In order to use this advice, your class needs to implement the ThrowsAdvice interface class. This will be after<br />
any exception occurs in your code. Now, remember that you added a new method in your SearchEngine<br />
interface (as shown in Listing 8-2). And in your implementation (as shown in Listing 8-3) you throw the<br />
UnsupportedOperationException. Let’s see how this will work (see Listing 8-12).<br />
Listing 8-12. ThrowsLoggingModule.java<br />
package com.apress.isf.spring.aop;<br />
import java.lang.reflect.Method;<br />
import org.slf4j.Logger;<br />
import org.slf4j.LoggerFactory;<br />
import org.springframework.aop.ThrowsAdvice;<br />
public class ThrowsLoggingModule implements ThrowsAdvice {<br />
private static final Logger log = LoggerFactory.getLogger(ThrowsLoggingModule.class);<br />
public void afterThrowing(Method m, Object[] args, Object target, Exception ex) {<br />
if(log.isDebugEnabled()){<br />
log.debug("@@@(THROWS) Method called: " + m.getName());<br />
if(args.length ==0 )<br />
log.debug("@@@@(THROWS) No arguments passed.");<br />
102