跟我学aspectj之九----- advice

 

asepctj有5种类型的advice

 

  • before( Formals )
  • after( Formals ) returning [ ( Formal ) ]
  • after( Formals ) throwing [ ( Formal ) ]
  • after( Formals )
  • Type around( Formals )

 

 

关于 前四种不想做过多的解释。before已经在我们之前的的Demo中用了无数次了,剩下的3个,我给一个基本的语法就可以了,用起来和before一样。

aspect A { pointcut publicCall(): call(public Object *(..)); after() returning (Object o): publicCall() { System.out.println("Returned normally with " + o); } after() throwing (Exception e): publicCall() { System.out.println("Threw an exception: " + e); } after(): publicCall(){ System.out.println("Returned or threw an Exception"); } }

 

如果不太清楚的同学,可以自己把我们之前的Demo改进,看看结果便清楚。接下来,我们重点讲讲around通知:

 

 

package com.aspectj.demo.aspect; import com.aspectj.demo.test.HelloAspectDemo; public aspect HelloAspect { pointcut HelloWorldPointCut(int x) : execution(* main(int)) && !within(HelloAspectDemo) && args(x); int around(int x) : HelloWorldPointCut(x){ System.out.println("Entering : " + thisJoinPoint.getSourceLocation()); int newValue = proceed(x*3); return newValue; } }

 

 

package com.aspectj.demo.test; public class HelloWorld { public static int main(int i){ System.out.println("in the main method i = " + i); return i; } /** * @param args */ public static void main(String[] args) { main(5); } }

 

最主要的就是 proceed()这个方法~ 重要的还是自己感觉一下吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值