Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 8 ■ Give Advice to Your Spring Application } } try{ engineProxy.findByLocation("/some/path/"); }catch(Exception ex){ log.error(ex.getMessage()); } If you run this test with gradle :ch08:test you will have the following output: 2014-02-23 20:04:06,217 DEBUG [main] Using Spring AOP: 2014-02-23 20:04:06,404 ERROR [main] findByLocation not yet implemented. Where are the logs? Well, you removed them from your implementation (see SearchEngineService.java, which is shown in Listing 8-3). You also added a new method in your interface (see Listing 8-2). And in your implementation you just throw an UnsupportedOperationException exception, that’s why you are seeing the error log. Next, let’s learn more about each advice type: before, after, around, and after throw. Before Advice You are going to create your first advice type. Spring AOP provides a MethodBeforeAdvice interface, and it provides a before method for its implementation. This method will be called before the object’s method execution (see Listing 8-6). In other words, you are intercepting a method call before its execution. Listing 8-6. BeforeLoggingModule.java package com.apress.isf.spring.aop; import java.lang.reflect.Method; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.aop.MethodBeforeAdvice; public class BeforeLoggingModule implements MethodBeforeAdvice { private static final Logger log = LoggerFactory.getLogger(BeforeLoggingModule.class); } 96 public void before(Method method, Object[] args, Object target) throws Throwable { if(log.isDebugEnabled()){ log.debug("@@@@(BEFORE) Method called: " + method.getName()); if(args.length ==0 ) log.debug("@@@@(BEFORE) No arguments passed."); for(Object arg:args) log.debug("@@@@(BEFORE) Argument passed:" + arg); } }
Chapter 8 ■ Give Advice to Your Spring Application How will this code interact with your SearchEngine implementation? Well, you only need to declare the BeforeLoggingModule class (see Listing 8-6) in your XML file, as well as declare a Proxy class (I am going to discuss this in the next section). Spring will take care of the rest. Next, you will add the beans you need, as shown in Listing 8-7. Listing 8-7. mydocuments-aop-context.xml beforeLogging You added your beforeLogging bean that points to your class where your interface MethodBeforeAdvice is implemented. Then there is a new bean, engineProxy, that points to the org.springframework.aop.framework. ProxyFactoryBean class. This class will create a proxy using the JDK reflection. Or you can choose to use a CGLib (Code Generation Library) that will help to create the necessary code to be in place when the methods are called. Because here you are using the CGLib, you set the property proxyTargetClass to true. You also defined two more properties, the target that refers to your SearchEngineService class and the property interceptorNames that has the value beforeLogging which matches with your bean definition or your advice. This will help the Spring container to advise all the methods in your SearchEngineService. If you run the same unit test (see Listing 8-5) using gradle :ch08:test you should have the following output: 2014-02-23 20:29:30,119 DEBUG [main] Using Spring AOP: 2014-02-23 20:29:30,152 DEBUG [main] @@@@(BEFORE) Method called: findByType 2014-02-23 20:29:30,152 DEBUG [main] @@@@(BEFORE) Argument passed:Type(name: WEB, description: Web Link, extension: .url) 97
- Page 45 and 46: Chapter 3 ■ Applying Different Co
- 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: Chapter 8 ■ Give Advice to Your S
- Page 99 and 100: 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
Chapter 8 ■ Give Advice to Your <strong>Spring</strong> Application<br />
How will this code interact with your SearchEngine implementation? Well, you only need to declare the<br />
BeforeLoggingModule class (see Listing 8-6) in your XML file, as well as declare a Proxy class (I am going to discuss<br />
this in the next section). <strong>Spring</strong> will take care of the rest. Next, you will add the beans you need, as shown in Listing 8-7.<br />
Listing 8-7. mydocuments-aop-context.xml<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
beforeLogging<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
You added your beforeLogging bean that points to your class where your interface MethodBeforeAdvice is<br />
implemented. Then there is a new bean, engineProxy, that points to the org.springframework.aop.framework.<br />
ProxyFactoryBean class. This class will create a proxy using the JDK reflection. Or you can choose to use a CGLib<br />
(Code Generation Library) that will help to create the necessary code to be in place when the methods are called.<br />
Because here you are using the CGLib, you set the property proxyTargetClass to true. You also defined two more<br />
properties, the target that refers to your SearchEngineService class and the property interceptorNames that has the<br />
value beforeLogging which matches with your bean definition or your advice. This will help the <strong>Spring</strong> container to<br />
advise all the methods in your SearchEngineService.<br />
If you run the same unit test (see Listing 8-5) using<br />
gradle :ch08:test<br />
you should have the following output:<br />
2014-02-23 20:29:30,119 DEBUG [main] Using <strong>Spring</strong> AOP:<br />
2014-02-23 20:29:30,152 DEBUG [main] @@@@(BEFORE) Method called: findByType<br />
2014-02-23 20:29:30,152 DEBUG [main] @@@@(BEFORE) Argument passed:Type(name: WEB, description:<br />
Web Link, extension: .url)<br />
97