AspectJ的注解开发AOP:环绕通知的学习

环绕通知的注解为@Around
around方法的返回值就是目标代理方法执行返回值
参数ProceedingJoinPoint可以拦截目标方法的执行
切面类配置如下
i

mport org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import java.math.BigInteger;

@Aspect
public class aspectj {
// @Before(value = "execution(* com.AspecJ.xiaomaoDao.*(..))")
// public void before(){
// System.out.println("我是前置通知!");
// }


// @AfterReturning(value = "execution(* com.AspecJ.xiaomaoDao.delete())",returning = "element")
// 使用returning来接受返回值
// public void After(Object element){
// System.out.println("我删除了"+element); //打印输出了返回值
// }

@Around(value="execution(* com.AspecJ.xiaomaoDao.update())")
public Object around(ProceedingJoinPoint joinPoint){
Object obj=null;
System.out.println("环绕前");
try {
obj=joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕后");
return obj;
}
}

 

目标类方法如下:

public class xiaomaoDao {
public void save(){
System.out.println("save xiaomao!");
}
public void find(){
System.out.println("find xiaomao!");
}
public String delete(){
System.out.println("delete xiaomao!");
return "xiaomao";
}
public void update(){
System.out.println("update xiaomao!");
}
}

 


执行结果如下:

delete xiaomao!
find xiaomao!
save xiaomao!
环绕前
update xiaomao!
环绕后

 

转载于:https://www.cnblogs.com/xiaolaha/p/11370854.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值