Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 8 ■ Give Advice to Your Spring Application } if(cache.containsKey(documentType.getName())){ log.debug("@@@(Caching) Found in Cache!"); return cache.get(documentType.getName()); } log.debug("@@@(Caching) Not Found! but is cachable!"); result = pjp.proceed(); cache.put(documentType.getName(), result); return result; } } return pjp.proceed(); In Listing 8-17 you added the @Aspect annotation to define your class as an aspect. You used the @Around annotation advice that accepts the pointcut as an expression; in this case, this will be read as “every method that is on SearchEngine with any return type and any arguments.” Listing 8-18 shows your XML; remember you are using annotations and you need to scan your packages so the Spring container knows about them. Listing 8-18. mydocuments-aop-annotated-context.xml In Listing 8-18, you added the and namespaces to your XML, and you added the tag to scan your packages. Then you added the tag; this tag will be doing the magic for the AOP to work. For this to run, you need some jar dependencies in your build.gradle file. See this book’s companion source code and the integration of these libraries (aspectjrt and aspectjweaver) for more information. 108
Chapter 8 ■ Give Advice to Your Spring Application You can run your unit test (Listing 8-16) with gradle :ch08:test and you should see the same output as the previous run. ■ ■Note The book’s companion source code shows the other methods Spring supports, such as Java configuration style, annotations and Groovy style. Check it out! Summary In this chapter, you saw how AOP (aspect-oriented programming) can help you separate and modularize your application concerns and get rid of scattering and tangling code. You don’t want to have code all over the place or some of your concerns mixed with different business logic that it doesn’t need to know about. If you need more information about the AspectJ and Spring Framework AOP extension, take a look at the Pro Spring and Spring Recipes series from Apress. You also saw all the advice types that Spring supports, as well as how to use them. You will continue to use AOP for the rest of the book. In later chapters, you will learn what to do when you need some caching, security, or transaction management. 109
- 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 and 98: 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: 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
- Page 149 and 150: Chapter 11 ■ Integrating Your Spr
- Page 151 and 152: Chapter 11 ■ Integrating Your Spr
- Page 153 and 154: Chapter 11 ■ Integrating Your Spr
- Page 155 and 156: Chapter 11 ■ Integrating Your Spr
- Page 157 and 158: Chapter 11 ■ Integrating Your Spr
Chapter 8 ■ Give Advice to Your <strong>Spring</strong> Application<br />
You can run your unit test (Listing 8-16) with<br />
gradle :ch08:test<br />
and you should see the same output as the previous run.<br />
■ ■Note The book’s companion source code shows the other methods <strong>Spring</strong> supports, such as Java configuration style,<br />
annotations and Groovy style. Check it out!<br />
Summary<br />
In this chapter, you saw how AOP (aspect-oriented programming) can help you separate and modularize your<br />
application concerns and get rid of scattering and tangling code. You don’t want to have code all over the place or<br />
some of your concerns mixed with different business logic that it doesn’t need to know about. If you need more<br />
information about the AspectJ and <strong>Spring</strong> <strong>Framework</strong> AOP extension, take a look at the Pro <strong>Spring</strong> and <strong>Spring</strong> Recipes<br />
series from Apress.<br />
You also saw all the advice types that <strong>Spring</strong> supports, as well as how to use them. You will continue to use<br />
AOP for the rest of the book. In later chapters, you will learn what to do when you need some caching, security, or<br />
transaction management.<br />
109