WEB开发网
开发学院WEB开发Jsp Second-generation aspect-oriented programming ... 阅读

Second-generation aspect-oriented programming By Dave Schweisguth

 2008-01-05 18:36:00 来源:WEB开发网   
核心提示:Dynamic AOP with JBoss AOP JBoss AOP is an AOP implementation developed by JBoss. While it came out of the desire to use AOP in the JBoss application server, it

Dynamic AOP with JBoss AOP
JBoss AOP is an AOP implementation developed by JBoss. While it came out of the desire to use AOP in the JBoss application server, it is an independent framework like aspectJ that can be used in any java PRogram. To see how it compares to AspectJ, let's jump right in to the same example recoded for AspectJ. We'll use all of the same code we used with the AspectJ example with a few exceptions. Here's what advice looks like in JBoss AOP:

public class ContextPasser implements Interceptor {
  public String getName() {
   return getClass().getName();
  }

  public Object invoke(Invocation invocation) throws Throwable {
   ServerSideContext.instance().setContext(
     ClientSideContext.instance().getContext());
   return invocation.invokeNext();
  }

}

JBoss AOP advice is simply a Java class that implements the interface org.jboss.aop.Interceptor. This interface has one trivial method: getName(), which is used for display, and one interesting method, invoke(Invocation), which is where we put the same context-passing code we put in the AspectJ advice. The last line of invoke(Invocation) returns control to the framework. Here it's just a bit of boilerplate, but in a different situation, we could replace the value returned from the actual method call with something else.

That's it! Advice is just a Java class in JBoss AOP, so there is no new syntax to learn and all of your development tools work with it like any other Java code. That takes care of the first objection we raised above.

But where's the equivalent to AspectJ's pointcut eXPression, which binds the advice to a method call? JBoss AOP provides two different ways to do this. The first uses a configuration file, usually called jboss-aop.xml:

<aop>
  <bind pointcut="execution(* *->@contextual(..))">
   <interceptor class="ContextPasser"/>
  </bind>
</aop>

This file is usually read at class load time, so we can add and remove advice and change the methods to which advice applies without recompiling. If we wish, we can compile our aspects instead, just as we did with AspectJ; this file will then be interpreted at compile time rather than at load time.

The other way to attach our advice is even more flexible. We still need a pointcut in jboss-aop.xml to tell JBoss AOP what methods we might want to advise:

<aop>

Tags:Second generation aspect

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接