Aspectj Cookbook

1.3. A Definition of Aspect Orientation

Before getting into the actual recipes, it is worth briefly introducing some of the concepts and terms that are used throughout this book.

Cross-Cutting Concerns

The basic premise of aspect-oriented programming is to enable developers to express modular cross-cutting concerns in their software. So what does this mean? A cross-cutting concern is behavior, and often data, that is used across the scope of a piece of software. It may be a constraint that is a characteristic of your software or simply behavior that every class must perform.

The most common example of a cross-cutting concern, almost the "Hello World" of the aspect-oriented approach, is that of logging (covered in Chapter 21). Logging is a cross-cutting concern because it affects many areas across the software system and it intrudes on the business logic. Logging is potentially applied across many classes, and it is this form of horizontal application of the logging aspect that gives cross-cutting its name.

Aspects

An aspect is another term for a cross-cutting concern. In aspect orientation, the aspects provide a mechanism by which a cross-cutting concern can be specified in a modular way. To fully harness the power of aspects, we need to have some basic concepts in place to allow us to specify and apply aspects in a generic manner. We must be able to:

  • Define the aspects in a modular fashion

  • Apply aspects dynamically

  • Apply aspects according to a set of rules

  • Provide a mechanism and a context for specifying the code that will be executed for that particular aspect

The aspect-oriented approach provides a set of semantics and syntactical constructs to meet these demands so that aspects can be applied generically regardless of the type of software being written. These constructs are advice, join points, and pointcuts.

Advice

The code that is executed when an aspect is invoked is called advice. Advice contains its own set of rules as to when it is to be invoked in relation to the join point that has been triggered.

Chapter 13 deals directly with recipes for different forms of advice and shows some of the more advanced features of advice that are available within AspectJ, such as precedence between multiple advices.

Join Points

Join points are simply specific points within the application that may or may not invoke some advice. The specific set of available join points is dependent on the tools being used and the programming language of the application under development. The following join points are supported in AspectJ:

  • Join when a method is called

  • Join during a method's execution

  • Join when a constructor is invoked

  • Join during a constructor's execution

  • Join during aspect advice execution

  • Join before an object is initialized

  • Join during object initialization

  • Join during static initializer execution

  • Join when a class's field is referenced

  • Join when a class's field is assigned

  • Join when a handler is executed

Pointcuts

Pointcuts are the AspectJ mechanism for declaring an interest in a join point to initiate a piece of advice. They encapsulate the decision-making logic that is evaluated to decide if a particular piece of advice should be invoked when a join point is encountered.

The concept of a pointcut is crucial to the aspect-oriented approach because it provides an abstract mechanism by which to specify an interest in a selection of join points without having to tie to the specifics of what join points are in a particular application.

How to define and use pointcuts is shown in the recipes found in Chapter 4 though Chapter 12.

Putting It All Together

Figure 1-1 shows the relationships between join points, aspects, pointcuts, advice, and your application classes.

Figure 1-1. The relationships between apects, pointcuts, and advice

Категории