Language Versus Framework
The designers of AspectJ elected to enhance the Java language to support AOP. Over the
However, changing the
Recent AOP
A main advantage of these frameworks is that they bring AOP to
AOP Framework Implementations
The number of open source AOP frameworks available is growing steadily as AOP continues to gain interest among the Java community. Table 11-1 lists a selection of the AOP frameworks available as
Table 11-1. AOP Frameworks
To contrast AspectJ against a framework AOP implementation, we look at the approach to defining aspects taken by AspectWerkz. |
Introducing AspectWerkz
AspectWerkz
is a dynamic AOP implementation for Java, jointly developed by Jonas Bonr and Alexandre Vasseur. The framework is
The AspectWerkz framework is not as comprehensive as AspectJ, but has a reputation as a
Note
Before the release of J2SE 5.0, AOP frameworks like AspectWerkz used XDoclet-style metatags embedded within Javadoc comment blocks. With
Chapter 6 provides an overview of metadata annotations .
To compare how the approach of the AspectWerkz framework to AOP
Listing 11-4. AspectWerkz AuditingAspect Class
package stock;
import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
public class AuditingAspect {
public void beforeAddStockItem(JoinPoint joinPoint) {
.
.
.
}
public void afterAddStockItem(JoinPoint joinPoint) {
.
.
.
}
}
The code snippet in Listing 11-4 is devoid of any AOP language constructs, although there are a few hints such as the JoinPoint class. We look first at how an external XML definition file transforms the class into a full-fledged aspect. XML Aspect DefinitionOne of the first approaches of many of the AOP frameworks was to declare pointcut designators and the location of advice methods using XML definition files. Listing 11-5 shows a sample of an AspectWerkz definition file that turns the plain AuditingAspect Java class into an aspect. Listing 11-5. XML Definition File aop.xml
<aspectwerkz>
<system id="stockcontrol">
<package name="stock">
<!-- Define the class implementing the aspect -->
<aspect class="stock.AuditingAspect"
deployment-model="perInstance">
<!-- Named pointcut -->
<pointcut name="auditStockItem"
expression="call(* Warehouse.add(..))"/>
<!-- Bind aspect before method to pointcut -->
<advice name="beforeAddStockItem"
type="before"
bind-to="auditStockItem"/>
<!-- Bind aspect after method to pointcut -->
<advice name="afterAddStockItem"
type="after"
bind-to="auditStockItem"/>
</aspect>
</package>
</system>
</aspectwerkz>
Comparing the aspect definition in the XML file against the example from AspectJ reveals many similarities. Most notable is the syntax for the pointcut declaration. For example, here is the declaration for the pointcut in AspectJ:
pointcut auditStockItem() :
call(* Warehouse.add(..));
Compare this to the corresponding declaration in the AspectWerkz configuration:
<pointcut name="auditStockItem"
expression=
"call(* Warehouse.add(..))"/>
Note
Supporters of AspectJ
Aspects as Metadata Annotations
Later versions of AspectWerkz offer an alternative mechanism for specifying aspects using metadata annotations. This method moves AspectWerkz closer to the AspectJ model by embedding AOP constructs as annotations within the language. The advantage of embedded annotations over an external XML file is that all code
Listing 11-6 shows the AuditingAspect class but this time adorned with AspectWerkz annotations. Listing 11-6. AuditingAspect Class with Metatagspackage stock; import org.codehaus.aspectwerkz.joinpoint.JoinPoint; /** * @Aspect perInstance */ public class AuditingAspect { /** * @Before call(* Warehouse.add(..)) */ public void beforeAddStockItem(JoinPoint joinPoint) { . . . } /** * @After call(* Warehouse.add(..)) */ public void afterAddStockItem(JoinPoint joinPoint) { . . . } } This new version of the AuditingAspect now bears a strong resemblance to the AspectJ version. Here is the declaration of the before advice from the AspectJ example:
before() :
auditStockItem() {...}
AspectWerkz produces a similar construct using metatags. The metadata also identifies the join points for the advice.
/**
*
@Before
call(* Warehouse.add(..))
*/
public void beforeAddStockItem(JoinPoint joinPoint) {...}
Metadata annotations replace the previous XML definition file, although a smaller file is still required to alert the AspectWerkz framework to the presence of aspects within the application.
Precompilation of AOP metadata information into the application makes it available at runtime for the AspectWerkz
Tip
The introduction of metadata annotations with J2SE 5.0 offers other interesting advantages to AOP frameworks. In addition to supporting the annotation model used by AspectWerkz, annotations make
AspectWerkz Weaving Options
The framework supports several weaving schemes, including bytecode and
Offline mode is compile-time weaving and is a two-step process in AspectWerkz. The first step is the standard compilation of all source code with
javac
. The final step is the weaving process. Here, the AspectWerkz framework
The online mode is more interesting, offering dynamic load-time weaving. In this mode, AspectWerkz hooks itself directly into the
Aspect-Oriented Middleware
AOP frameworks like AspectWerkz are gaining recognition for the advantages they bring to the development of enterprise software. The binding of application servers with AOP frameworks has given rise to a new
The J2EE platform already takes care of many major system-level crosscutting concerns for the developer, providing services for handling threading, security, persistence, transactions, and component-location transparency. Despite this long list of services, the J2EE platform employs a static model for the management of crosscutting concerns. In comparison to AOP-based frameworks, the J2EE model is rigid and
Major J2EE server
Aspect-oriented middleware is at the (cross) cutting edge and may in the future represent a fundamental change in the way we develop enterprise software. |