Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

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

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

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

Saved successfully!

Ooh no, something went wrong!