Chapter 4. Modifying behavior with dynamic crosscutting

 

This chapter covers

  • Learning advice basics
  • Using three advice types
  • Writing a caching example

In the previous chapter, we presented the join point model and examined the pointcut construct as the way to select join points of interest. In this chapter, you’ll put that knowledge to practical use as we discuss dynamic crosscutting. Dynamic crosscutting constructs provide a way to affect the behavior of a system, whereby you can alter behavior at join points selected by pointcuts. After reading this chapter, you should be able to start writing meaningful programs in AspectJ.

As we discussed in chapter 2, AspectJ supports multiple syntaxes: traditional and @AspectJ. Although the details vary a bit, all of these syntaxes share a common base. After you learn the traditional syntax, you can easily learn the @AspectJ syntax. Spring-AspectJ integration also uses many of the same base constructs, so when you know an AspectJ syntax form, you can easily apply it in Spring. In this chapter, we’ll continue to focus on the traditional syntax.

4.1. Advice overview

Dynamic crosscutting weaving rules consist of two parts: advice (what to do) and pointcuts (when to apply the advice). AspectJ supports dynamic crosscutting through advice—a method-like construct that defines crosscutting action at the join points selected by a pointcut. We’ll begin with discussing three kinds of advice followed by the general anatomy of advice.

4.2. Comparing advice to methods

4.3. Advice in depth

4.4. Collecting join point context

4.5. Accessing join point context via reflection

4.6. Summary