Aspect can clear code pollution? Logging with the Aspectwerkz framework is the first step
Introduction: Aspect-oriented programming (aspect-oriented PROGRAMMING,AOP) allows code to be kept clean by separating functionality that works within the application scope rather than in a class or package. Logging is a common example of this functionality. Can AOP help with logging? In this article, developers and AOP advocates Dennis Sosnoski analyze the use of the ASPECTWERKZ framework to find answers.
In my article on the dynamics of Java programming, I showed how to implement the system change of program behavior with classworking technology. This approach is the basis for most of the work done with object-oriented programming (AOP) on the Java platform. In this article, I'll show you how to build a higher level with the AOP framework on top of the classworking to achieve the same system changes, thereby improving ease of use.
Aspectwerkz Introduction
The Java platform has many AOP frameworks. In this series of articles, I chose to use the Aspectwerkz framework, an open source project sponsored by BEA Systems. Aspectwerkz is not the first aop--for Java Technology The honor belongs to the AspectJ extension of the IBM-supported Java programming language--but as a fast, powerful, and flexible AOP framework that combines standard Java code, ASPECTW Erkz has gained more and more attention. The Aspectwerkz and AspectJ teams recently announced a merger of their work, so in the future we will see the best features of both Java programming AOP approaches combined into one product. I'll keep track of that in this column, but now I'm going to stick with the basic Aspectwerkz framework.
Note: Don't miss the new Aop@work column, which is a good introduction to the actual use of AOP. It started in February 2005 and was written by five important AOP experts, and each article in this one-year series will provide knowledge that can be applied immediately.
When AOP advocates list the AOP applications that are being developed on a daily basis, it seems that logging is always used. Logging is used in a large part of the typical enterprise application code, which is clearly something to be concerned about. The logging code has nothing to do with the main purpose of the application, and it can also cause significant interference in the code. These features combine to make logging a good candidate for building aspect. I'll analyze a aspect that combines logging and performance metrics to see how the aspect paradigm applies to this type of problem.
Aspectwerkz Foundation
The Aspectwerkz Web site provides a wide range of documentation and usage examples, as well as links to articles about using Aspectwerkz. I'm not going to repeat this here, but I'll briefly introduce what I think is most important: pointcut, aspect and advice.
Pointcut is basically just a place to interrupt the "normal" flow of an application (defined by the source code) and do something different. Aspectwerkz supports many types of pointcut related to the Java code structure, including method invocation or execution, field Get/set and exception handler execution, and combinations (including code in a particular execution path).
In aspectwerkz terms, a aspect is a Java class that can serve as a target for cross attention (crosscutting concern). Making a class aspect does not require a special interface to be implemented, but a specific method signature is required to make the class accessible as a aspect. In general, you want the aspect classes to be outside the standard application class hierarchy because they may only be used through the ASPECTWERKZ framework, but there is no special requirement for where they are located (except in the Run-time classpath).
Advice is a method in the aspect class that uses it at pointcut to do "some different things". The default method signature for advice accepts a parameter that provides information about the interrupted pointcut. Listing 1 shows an example of a aspect class that defines two methods that will be used as front advice and post advice on a method call.