Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 47 and 48: Chapter 3 ■ Applying Different Co
- Page 49 and 50: Chapter 4 ■ Using Beans Scopes pu
- Page 51 and 52: Chapter 4 ■ Using Beans Scopes Th
- Page 53 and 54: Chapter 4 ■ Using Beans Scopes @A
- Page 55 and 56: Chapter 4 ■ Using Beans Scopes do
- Page 57 and 58: Chapter 5 ■ Working with Collecti
- Page 59 and 60: Chapter 5 ■ Working with Collecti
- Page 61 and 62: Chapter 5 ■ Working with Collecti
- Page 63 and 64: Chapter 5 ■ Working with Collecti
- Page 65 and 66: Chapter 6 ■ Using Resource Files
- Page 67 and 68: Chapter 6 ■ Using Resource Files
- Page 69 and 70: Chapter 6 ■ Using Resource Files
- Page 71 and 72: Chapter 6 ■ Using Resource Files
- Page 73 and 74: Chapter 6 ■ Using Resource Files
- Page 75 and 76: Chapter 6 ■ Using Resource Files
- Page 77 and 78: Chapter 7 Testing Your Spring Appli
- Page 79 and 80: Chapter 7 ■ Testing Your Spring A
- Page 81 and 82: Chapter 7 ■ Testing Your Spring A
- Page 83 and 84: Chapter 7 ■ Testing Your Spring A
- Page 85 and 86: Chapter 7 ■ Testing Your Spring A
- Page 87 and 88: Chapter 7 ■ Testing Your Spring A
- Page 89 and 90: Chapter 8 Give Advice to Your Sprin
- Page 91 and 92: Chapter 8 ■ Give Advice to Your S
- Page 93 and 94: Chapter 8 ■ Give Advice to Your S
- Page 95 and 96: Chapter 8 ■ Give Advice to Your S
- Page 97: Chapter 8 ■ Give Advice to Your S
- Page 101 and 102: Chapter 8 ■ Give Advice to Your S
- Page 103 and 104: Chapter 8 ■ Give Advice to Your S
- Page 105 and 106: Chapter 8 ■ Give Advice to Your S
- Page 107 and 108: Chapter 8 ■ Give Advice to Your S
- Page 109 and 110: Chapter 8 ■ Give Advice to Your S
- Page 111 and 112: Chapter 9 ■ Adding Persistence to
- Page 113 and 114: Chapter 9 ■ Adding Persistence to
- Page 115 and 116: Chapter 9 ■ Adding Persistence to
- Page 117 and 118: Chapter 9 ■ Adding Persistence to
- Page 119 and 120: Chapter 9 ■ Adding Persistence to
- Page 121 and 122: Chapter 9 ■ Adding Persistence to
- Page 123 and 124: Chapter 9 ■ Adding Persistence to
- Page 125 and 126: Chapter 9 ■ Adding Persistence to
- Page 127 and 128: Chapter 9 ■ Adding Persistence to
- Page 129 and 130: Chapter 9 ■ Adding Persistence to
- Page 131 and 132: Chapter 9 ■ Adding Persistence to
- Page 133 and 134: Chapter 10 ■ Showing Your Spring
- Page 135 and 136: Chapter 10 ■ Showing Your Spring
- Page 137 and 138: Chapter 10 ■ Showing Your Spring
- Page 139 and 140: Chapter 10 ■ Showing Your Spring
- Page 141 and 142: Chapter 10 ■ Showing Your Spring
- Page 143 and 144: Chapter 10 ■ Showing Your Spring
- Page 145 and 146: Chapter 11 ■ Integrating Your Spr
- Page 147 and 148: Chapter 11 ■ Integrating Your Spr
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