信息化 频道

两次飞跃创辉煌

    绑定一个导言

    好了,所以我们定义跟踪接口,并且实现这个混合类。下一步是应用导言到POJO类。像拦截器,我们必须在XML中定义一个ponitcut。让我们看一下这项什么。
<?xml version="1.0" encoding="UTF-8">   <aop>     <introduction-pointcut class="POJO">      <mixin>        <interfaces>Tracing</interfaces>        <class>TracingMixin</class>        <construction>new TracingMixin(this)</construction>      </mixin>     </introduction-pointcut>   </aop>
    上面的pointcuts将强制POJO类实现Tracing接口。现在,当一个POJO实例被初始化,一个TracingMixin也将被实例化。 TracingMixin被初始化的途径被定义在<contstruction>标签中。你能够把想要的任一行Java代码放入在< contstruction>标签中。
  
    运行例子3
    POJO类为了显示TracingAPI怎么被访问,它已经被扩展了一点。TracingInterceptor仍然和例子2一样。
[code]public class POJO
  {
   
public POJO() {}
   
public void helloWorld() { System.out.println(Hello World!); }
  
   
public static void main(String[] args)
    {
     POJO pojo  
= new POJO();
     Tracing trace
= (Tracing)this;
     pojo.helloWorld();
  
     System.
out.println("Turn off tracing.");
  
     trace.disableTracing();
     pojo.helloWorld();
  
     System.
out.println("Turn on tracing.");
  
     trace.enableTracing();
     pojo.helloWorld();
    }
  }
  [
/code]
    注意我们转换POJO到Tracing接口。输出应该看起来这样:
Entering constructor: POJO()
  Leaving constructor: POJO()
  Entering method: helloWorld
  Hello World
!   Leaving method: helloWorld   Turn off tracing.   Entering method: disableTracing   Leaving method: disableTracing   Hello World!   Turn on tracing.   Entering method: helloWorld   Hello World!   Leaving method: helloWorld
    注意被增加到TracingInterceptor 中的interceptor-pointcut也应用到那些通过Tracing 导言导入的方法中。
    为了编译和运行这个例子:
$ cd oreilly-aop/example3
  $ export CLASSPATH
=.;jboss-common.jar;jboss-aop.jar;javassist.jar
  $ javac
*.java
  $ java
-Djava.system.class.loader=org.jboss.aop.standalone.SystemClassLoader POJO
    结论

    面向方面编程对于软件开发是一个强有力的新工具。为了使你的软件开发过程更加动态和流畅,用JBoss4.0,你能够实现你自己的拦截器,元数据和导言。更详细的文档参见我们的站点www.jboss.org。那会有一些惊奇等着你,象我们已经在我们新的框架上实现了一套服务。拥有它并恰当的使用。
0
相关文章