1. AspectJ是一个代码生成工具(Code Generator)。
  2. AspectJ语法就是用来定义代码生成规则的语法。您如果使用过Java Compiler Compiler (JavaCC),您会发现,两者的代码生成规则的理念惊人相似。
  3. AspectJ有自己的语法编译工具,编译的结果是Java Class文件,运行的时候,classpath需要包含AspectJ的一个jar文件(Runtime lib)。
  4. ....
看了上面几点,我就想看看它怎么把代码生成了。现在做一个试验。
一个类(包括main函数):Speaker.java
明明白白AspectJ _职场package test.aspectj;
明明白白AspectJ _职场
明明白白AspectJ _职场
public class Speaker
明明白白AspectJ _休闲_04
{
明明白白AspectJ _休闲_07    
public void speak()
明明白白AspectJ _开发_08    
{
明明白白AspectJ _休闲_07        System.out.println(
"[Speaker] bla bla 明明白白AspectJ _休闲_06");
明明白白AspectJ _职场_13    }

明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
public static void main(String[] args)
明明白白AspectJ _开发_16    
{
明明白白AspectJ _休闲_07        Speaker speaker 
= new Speaker();
明明白白AspectJ _休闲_07        speaker.speak();
明明白白AspectJ _职场_13    }

明明白白AspectJ _职场_22}
 
一个aspect:AspectObserver.aj
明明白白AspectJ _职场package test.aspectj;
明明白白AspectJ _职场
明明白白AspectJ _职场
public aspect AspectObserver
明明白白AspectJ _休闲_26
{
明明白白AspectJ _休闲_07    pointcut speakerSpeak():
明明白白AspectJ _休闲_07        call(
void *Speaker.speak());
明明白白AspectJ _休闲_31    before() : speakerSpeak() 
{
明明白白AspectJ _休闲_07        System.out.println(
"[AspectObserver] speaker is about to speak!");
明明白白AspectJ _职场_13    }

明明白白AspectJ _职场_36    after() returning() : speakerSpeak() 
{
明明白白AspectJ _休闲_07        System.out.println(
"[AspectObserver] speaker has completed his speech!");
明明白白AspectJ _职场_13    }

明明白白AspectJ _职场_22}
以上都是源码部分哦。
运行结果:
明明白白AspectJ _职场[AspectObserver] speaker is about to speak!
明明白白AspectJ _职场[Speaker] bla bla 明明白白AspectJ _休闲_06
明明白白AspectJ _职场[AspectObserver] speaker has completed his speech
!
说明程序是正常运作的哦。
好了,下面,做三个操作:
1、将以上的编译成的class文件打包成apectjtest.jar文件。
      说明:可以使用ajdt的eclipse插件带的导出功能,Export -->  JAR file with ApectJ support
2、新建一个AspectJ工程,将apectjtest.jar加入类路径,使用jad来反编译Speaker.class和AspectObserver.class
 得到反编译后的源码:
Speaker.class
明明白白AspectJ _休闲_46/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
明明白白AspectJ _职场
// Jad home page: [url]http://www.kpdus.com/jad.html[/url]
明明白白AspectJ _职场
// Decompiler options: packimports(3) radix(10) lradix(10) 
明明白白AspectJ _职场
// Source File Name:   Speaker.java
明明白白AspectJ _职场

明明白白AspectJ _职场
package test.aspectj;
明明白白AspectJ _职场
明明白白AspectJ _职场
import java.io.PrintStream;
明明白白AspectJ _职场
明明白白AspectJ _职场
// Referenced classes of package test.aspectj:
明明白白AspectJ _职场
//            AspectObserver
明明白白AspectJ _职场

明明白白AspectJ _职场
public class Speaker
明明白白AspectJ _休闲_60
{
明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
public Speaker()
明明白白AspectJ _开发_65    
{
明明白白AspectJ _职场_13    }

明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
public void speak()
明明白白AspectJ _开发_71    
{
明明白白AspectJ _休闲_07        System.out.println(
"[Speaker] bla bla 明明白白AspectJ _休闲_06");
明明白白AspectJ _职场_13    }

明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
public static void main(String args[])
明明白白AspectJ _休闲_79    
{
明明白白AspectJ _休闲_07        Speaker speaker 
= new Speaker();
明明白白AspectJ _休闲_07        AspectObserver.aspectOf().ajc$before$test_aspectj_AspectObserver$
1$b2b6354();
明明白白AspectJ _休闲_07        speaker.speak();
明明白白AspectJ _休闲_07        AspectObserver.aspectOf().ajc$afterReturning$test_aspectj_AspectObserver$
2$b2b6354();
明明白白AspectJ _职场_13    }

明明白白AspectJ _职场_22}
 
AspectObserver.class
明明白白AspectJ _休闲_46/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
明明白白AspectJ _职场
// Jad home page: [url]http://www.kpdus.com/jad.html[/url]
明明白白AspectJ _职场
// Decompiler options: packimports(3) radix(10) lradix(10) 
明明白白AspectJ _职场
// Source File Name:   AspectObserver.aj
明明白白AspectJ _职场

明明白白AspectJ _职场
package test.aspectj;
明明白白AspectJ _职场
明明白白AspectJ _职场
import java.io.PrintStream;
明明白白AspectJ _职场
import org.aspectj.lang.NoAspectBoundException;
明明白白AspectJ _职场
明明白白AspectJ _职场
public class AspectObserver
明明白白AspectJ _职场_100
{
明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
public AspectObserver()
明明白白AspectJ _休闲_105    
{
明明白白AspectJ _职场_13    }

明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
void ajc$pointcut$$speakerSpeak$44()
明明白白AspectJ _休闲_111    
{
明明白白AspectJ _职场_13    }

明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
public void ajc$before$test_aspectj_AspectObserver$1$b2b6354()
明明白白AspectJ _职场_117    
{
明明白白AspectJ _休闲_07        System.out.println(
"[AspectObserver] speaker is about to speak!");
明明白白AspectJ _职场_13    }

明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
public void ajc$afterReturning$test_aspectj_AspectObserver$2$b2b6354()
明明白白AspectJ _开发_124    
{
明明白白AspectJ _休闲_07        System.out.println(
"[AspectObserver] speaker has completed his speech!");
明明白白AspectJ _职场_13    }

明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
public static AspectObserver aspectOf()
明明白白AspectJ _开发_131    
{
明明白白AspectJ _休闲_07        
if(ajc$perSingletonInstance == null)
明明白白AspectJ _休闲_07            
throw new NoAspectBoundException("test_aspectj_AspectObserver", ajc$initFailureCause);
明明白白AspectJ _休闲_07        
else
明明白白AspectJ _休闲_07            
return ajc$perSingletonInstance;
明明白白AspectJ _职场_13    }

明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
public static boolean hasAspect()
明明白白AspectJ _职场_141    
{
明明白白AspectJ _休闲_07        
return ajc$perSingletonInstance != null;
明明白白AspectJ _职场_13    }

明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
private static void ajc$postClinit()
明明白白AspectJ _开发_148    
{
明明白白AspectJ _休闲_07        ajc$perSingletonInstance 
= new AspectObserver();
明明白白AspectJ _职场_13    }

明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
private static Throwable ajc$initFailureCause;
明明白白AspectJ _休闲_07    
public static final AspectObserver ajc$perSingletonInstance;
明明白白AspectJ _休闲_07
明明白白AspectJ _休闲_07    
static 
明明白白AspectJ _开发_158    
{
明明白白AspectJ _休闲_07        
try
明明白白AspectJ _开发_162        
{
明明白白AspectJ _休闲_07            ajc$postClinit();
明明白白AspectJ _职场_13        }

明明白白AspectJ _休闲_07        
catch(Throwable throwable)
明明白白AspectJ _休闲_168        
{
明明白白AspectJ _休闲_07            ajc$initFailureCause 
= throwable;
明明白白AspectJ _职场_13        }

明明白白AspectJ _职场_13    }

明明白白AspectJ _职场_22}
3、运行一下Speaker.class
得到结果:
明明白白AspectJ _职场[AspectObserver] speaker is about to speak!
明明白白AspectJ _职场[Speaker] bla bla 明明白白AspectJ _休闲_06
明明白白AspectJ _职场[AspectObserver] speaker has completed his speech
!
 
呵呵,结果跟源码运行是一样的哦(不一样就是你的人品问题咯!!)
分析一下,先理解AspectJ编译器为我们做了什么事情:
明明白白AspectJ _职场首先、AspectJ从文件列表里取出所有的文件名,然后读取这些文件,进行分析。 
明明白白AspectJ _职场二、AspectJ发现一些文件含有aspect的定义,在这个例子里,就是AspectObserver的定义;这些aspect就是代码生成规则。 
明明白白AspectJ _职场三、AspectJ根据这些aspect代码生成规则,修改添加你的源代码。在这个例子里,源码是修改成怎样了?比较一下反编译后的代码和源码便知。 
明明白白AspectJ _职场四、AspectJ读取AspectObserver的定义,发现了一个pointcut
--speakerSpeak();这个pointcut的定义是call(void *Speaker.speak()),表示所有对Speaker类的speak方法的执行点。 
明明白白AspectJ _职场五、AspectJ继续读取AspectObserver的定义,发现了一个before(),这在AspectJ中叫做Advice。Advice允许你在某个类的方法的调用之前或调用之后,加入另外的代码。加入的代码是什么?比较一下反编译后的代码和源码吧。 AspectJ继续读取AspectObserver的定义明明白白AspectJ _休闲_06.
好了,回头看看反编译后的Speaker.class,与源码Speaker.java差别在哪呢?主要在main函数中,方法被调用的前后
明明白白AspectJ _职场    public static void main(String args[])
明明白白AspectJ _开发_186    
{
明明白白AspectJ _休闲_07        Speaker speaker 
= new Speaker();
明明白白AspectJ _休闲_07        AspectObserver.aspectOf().ajc$before$test_aspectj_AspectObserver$
1$b2b6354();//多了这行
明明白白AspectJ _休闲_07
        speaker.speak();
明明白白AspectJ _休闲_07        AspectObserver.aspectOf().ajc$afterReturning$test_aspectj_AspectObserver$
2$b2b6354();//多了这行
明明白白AspectJ _职场_22
    }
不同的地方就是多出了两行,虽然有$和数字,但是很容易看出,这同我们平常写的java代码差不了多少。
明明白白AspectJ _职场        AspectObserver.aspectOf().ajc$before$test_aspectj_AspectObserver$1$b2b6354();
这行代码中,看起来不就是AspectObserver类调用了静态方法aspectOf()吗,接着aspectOf()的返回对象又调用ajc$before$test_aspectj_AspectObserver$1$b2b6354()方法吗?
回来看看AspectObserver.class的反编译代码,哈哈,这就对了,aspectOf返回的是AspectObserver的一个实例,返回实例在调用实例方法ajc$before$test_aspectj_AspectObserver$1$b2b6354()。ajc$before$test_aspectj_AspectObserver$1$b2b6354()是干什么的呢?呵呵,不正是我们要的拦截方法所要做的操作吗?
明明白白AspectJ _职场System.out.println("[AspectObserver] speaker is about to speak!");
同样,可知
明明白白AspectJ _职场 AspectObserver.aspectOf().ajc$afterReturning$test_aspectj_AspectObserver$2$b2b6354();
是怎么一回事啦。
这正是应了上面所提的两点:
  1. AspectJ是一个代码生成工具(Code Generator)。
  2. AspectJ语法就是用来定义代码生成规则的语法。
用这两点去感受apectj在程序里该如何去运用使用,就清晰多啦!