Basic Concepts of AOP


This section first introduces some basic AOP concepts via some examples. These concepts and terms are used frequently throughout the rest of this chapter.

Joinpoints and Invocation

A joinpoint is simply any point in a Java program: The call of a method, the execution of a constructor, and the access of a field are all joinpoints. Joinpoints are places where aspects provide services to the object. The following is an example of the POJO Foo:

 public class Foo {   public int fooField;   public Foo () {     fooField = 0;   }   public String fooMethod (int i) {     return Integer.toString (fooField + i);   } } 

The following actions on the Foo class and its instance objects are joinpoints:

 Foo foo = new Foo (); int k = foo.fooField; String s = foo.fooMethod (0); 

Inside the JBoss AOP, a joinpoint is encapsulated by an Invocation object at runtime. This object could contain information such as which method is being called, the arguments of the method, and so on. It is available to developers via the JBoss AOP framework API.

Advice and Aspects

The AOP system dynamically modifies an object's behavior at joinpoints. The injected behavior, such as logging, security checking, transactions, and so on, is specified in regular Java methods known as advices. In the following example, the trace() method is an advice:

 public class FooAspect {   public Object trace (Invocation invocation) throws Throwable {     try {       System.out.println("Enter the joinpoint");       return invocation.invokeNext ();     } finally {       System.out.println("Leave the joinpoint");     }   } } 

When the execution flow reaches a joinpoint that is bound to the trace() advice (you will see how to specify the binding later), the JVM executes the trace() method instead of the joinpoint. Here, the TRace() method prints out a message, instructs the AOP runtime to execute the joinpoint, and then prints another message. The invocation object contains information about the joinpoint where this advice is applied. You can apply the advice to any joinpoint to log an event.

In JBoss AOP, an aspect class is a regular Java class that holds one or many advice methods. For example, the FooAspect class is an aspect. A special kind of aspect class in JBoss AOP is an interceptor; it must implement the Interceptor interface, which defines only one advice method: invoke(). This interface helps developers enforce compile-time type checking for the interceptor type of aspect classes.

Introducing Pointcuts

An advice is bound to a specific set of joinpoints known as a pointcut. As a developer, you specify the mapping rules to group joinpoints into pointcuts, using an expression language supported by JBoss AOP. As discussed in the following sections, there are three ways to specify a pointcut:

  • Using an XML configuration file

  • Using annotations

  • Using annotation in application classes

Using an XML Configuration File

The following example from the jboss-aop.xml descriptor specifies that the TRace() advice is bound to the Foo . fooMethod() method call joinpoint:

 <aop>   <aspect  scope="PER_VM"/>       <bind pointcut="execution(public String Foo->fooMethod(..))">     <advice name="trace" aspect="FooAspect"/>   </bind> </aop> 

You can find a complete reference of the elements in the jboss-aop.xml file in the JBoss AOP manual.

Using Annotations

If you do not want to manage a separate jboss-aop.xml configuration file, you can declare the aspect and specify its bindings in the aspect class's source code, by using annotations. In JDK 5.0, annotations are an officially supported Java language feature. You can just use JBoss AOPdefined annotations to tag your aspect class and advice methods, as in the following example:

 @Aspect (scope = Scope.PER_VM) public class FooAspect {   @Bind (pointcut="execution("* Foo->fooMethod())")   public Object trace (Invocation invocation) throws Throwable {     try {       System.out.println("Enter the joinpoint");       return invocation.invokeNext ();     } finally {       System.out.println("Leave the joinpoint");     }   } } 

In JDK 1.4, however, annotations are not supported by the Java compiler. JBoss AOP allows you to embed the annotations in JavaDoc comments. You can use the JBoss annotation compiler to extract an annotation from the source code and then add it to the compiled bytecode files or store it in a separate XML file for further processing.

  /**   * @@Aspect (scope = Scope.PER_VM)   */ public class FooAspect {   /**     * @@org.jboss.aop.Bind (pointcut="execution("* Foo->fooMethod())")     */   public Object trace (Invocation invocation) throws Throwable {     try {       System.out.println("Enter the joinpoint");       return invocation.invokeNext ();     } finally {       System.out.println("Leave the joinpoint");     }   } } 

Annotations are easier to use than the jboss-aop.xml configuration file because they are closer to the source code they are supposed to control.

Using Annotation in Application Classes

So far, you have seen how to bind advices to pointcuts by using the signature pattern of the joinpoints (for example, the method signature). A more general way to specify pointcuts is to directly tag the joinpoints in the application code by using annotations. Then, in the jboss-aop.xml file, you can map the annotations to the advices. In the following jbossaop.xml file, the trace() advice is mapped to the @FooTrace annotation tag:

 <aop>   <aspect  scope="PER_VM"/>   <bind pointcut="execution(* *->@org.jboss.FooTrace(..))">     <advice name="trace" aspect="FooAspect"/>   </bind> </aop> 

Here is the application code that makes use of the @FooTrace annotation in JDK 5.0:

 public class Foo {   public int fooField;   public Foo () {     fooField = 0;   }   @FooTrace public String fooMethod (int i) {     return Integer.toString (fooField + i);   } } The version in JDK 1.4 with the JBoss annotation compiler is as follows: public class Foo {   public int fooField;      public Foo () {     fooField = 0;   }   /**     * @@org.jboss.FooTrace     */   public String fooMethod (int i) {     return Integer.toString (fooField + i);   } } 

Notice that you do not need to annotate the aspect class in this setup. The ability to specify pointcuts via annotations in the application code allows you to develop prepackaged aspects and then publish the annotation API for all to use. That is exactly what JBoss did to support prepackaged EJB-style AOP services inside the JBoss application server.

Introductions and Mixins

The aspects you have seen so far are interceptor types of aspects. Another key AOP feature is an introduction or a mixin of classes from independent inheritance trees. An introduction modifies the type and structure of a Java class. It can be used to force an existing class to implement an interface, by using methods from another class. It essentially allows developers to create C++-style multiple-inheritance object systems in Java. The following example shows that the methods in the FooMixin class are used to make the Foo class implement the FooMixinInt interface at runtime. Here is the FooMixinInt interface:

 public interface FooMixinInt {   public String fooMethod2 (int i); } 

The FooMixin class implements the FooMixinInt interface as follows:

 public class FooMixin implements FooMixinInt {   public String fooMethod2 (int i) {     return Integer.toString (fooField - i);   } }> 

However, the Foo class does not implement the FooMixinInt interface. The following jboss-aop.xml file forces the Foo class to implement FooMixinInt by using the method from the FooMixin class:

 introduction >   <mixin>     <interfaces>FooMixinInt</interfaces>     <class>FooMixin</class>     <construction>new FooMixin(this)</construction>   </mixin> </introduction> 

Then, in the application code, you can cast a Foo instance to the FooMixinInt type at runtime:

 Foo foo = new Foo (); FooMixinInt fooint = (FooMixinInt) foo; String s = foo.fooMethod2 (-2); 

The fooMethod2() method, which is defined in the FooMixin class but not in the Foo class, is now available in the Foo instance at the AOP runtime.



JBoss 4. 0(c) The Official Guide
JBoss 4.0 - The Official Guide
ISBN: B003D7JU58
EAN: N/A
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net