AspectJ的Eclipse插件下载:http://www.eclipse.org/aspectj/downloads.php
测试代码如下:
普通的Java类:
package
demo;

public
class
MyClass
...
{

public void foo(int number, String name)...{
System.out.println("Inside foo (int, String)");
}

public static void main(String[] args) ...{
MyClass myObject = new MyClass();
myObject.foo(1, "Russ Miles");
}
}
aspectj类:
HelloWorld.aj
package
demo;

public
aspect HelloWorld
...
{
pointcut calPointcut() :
call (void demo.MyClass.foo(int, String));

before() : calPointcut()...{
System.out.println("aspect call");
}
}
ExecutionRecipe.aj
package
demo;

public
aspect ExecutionRecipe
...
{
pointcut executionPointcut() :
execution (void demo.MyClass.foo(int, String));

before() : executionPointcut()...{
System.out.println("aspect execution");
}
}
AdviceExecutionRecipe.aj
package
demo;

public
aspect AdviceExecutionRecipe
...
{
pointcut adviceexecutionPointcut() : adviceexecution();

before() : adviceexecutionPointcut() && !within(AdviceExecutionRecipe +)...{
System.out.println("aspect adviceexecution");
}
}
输出:
aspect adviceexecution
aspect call
aspect adviceexecution
aspect execution
Inside foo (int, String)
本文通过具体的示例代码展示了如何使用AspectJ实现AOP编程。包括了不同的切入点定义方式,如调用(call)、执行(execution)及通知(adviceexecution),并通过实际运行结果验证了这些切入点的应用效果。
469

被折叠的 条评论
为什么被折叠?



