Spring AOP : Advice 声明 (通知注解)

Advice 的类型

before advice:在 join point 执行前被执行的 advice. 虽然 before advice 是在 join point 前被执行, 但是它并不能够阻止 join point 的执行, 除非发生了异常(即我们在 before advice 代码中, 不能人为地决定是否继续执行 join point 中的代码)
after return advice: 在一个 join point 正常返回后执行的 advice
after throwing advice: 当一个 join point 抛出异常后执行的 advice
after(final) advice:无论一个 join point 是正常退出还是发生了异常, 都会被执行的 advice.
around advice:在 join point 前和 joint point 退出后都执行的 advice. 这个是最常用的 advice.若切面包括该方法,那么必须调用ProceedingJoinPoint的proceed()方法才会执行目标方法。可以控制是否执行目标方法。

通知执行的优先级:
正常:
before around –> before –> after around –>afterReturning –> after
异常:
before around –> before –> after –> afterThrowing

简单样例

package com.ailianshuo.springaop.sample04;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
 *  通知类,横切逻辑
 * @author ailianshuo
 * 2017年7月25日 下午5:21:42
 * 
 * 
 * 
 */
@Component
@Aspect
public class Advices {

    /**
     * 定义一个切点然后复用
     */
    @Pointcut("execution(* com.ailianshuo.springaop.sample04.Math.*(..))")
    public void pointcutMath(){
    }

    @Before("pointcutMath()")
    public void before(JoinPoint jp){
        System.out.println("----------before advice----------");
        System.out.println(jp.getSignature().getName());
    }

    @After("pointcutMath()")
    public void after(JoinPoint jp){
        System.out.println("----------after advice----------");
    }



    //环绕通知
    @Around("execution(* com.ailianshuo.springaop.sample04.Math.s*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println(pjp.getSignature().getName());
        System.out.println("----------before around advice----------");
        Object result=pjp.proceed();
        System.out.println("----------after around advice----------");
        return result;
    }

    //返回结果通知
    @AfterReturning(pointcut="execution(* com.ailianshuo.springaop.sample04.Math.m*(..))",returning="result")
    public void afterReturning(JoinPoint jp,Object result){
        System.out.println(jp.getSignature().getName());
        System.out.println("afterReturning:"+result);
        System.out.println("----------after afterReturning advice----------");
    }

    //异常后通知
    @AfterThrowing(pointcut="execution(* com.ailianshuo.springaop.sample04.Math.d*(..))",throwing="exp")
    public void afterThrowing(JoinPoint jp,Exception exp){
        System.out.println(jp.getSignature().getName());
        System.out.println("Exception:"+exp.getMessage());
        System.out.println("----------Exception advice----------");
    }
}

运行结果

****************************** Add ******************************
----------before advice----------
add
10+5=15
----------after advice----------

****************************** sub ******************************
sub
----------before around advice----------
----------before advice----------
sub
10-5=5
----------after around advice----------
----------after advice----------

****************************** mut ******************************
----------before advice----------
mut
10X5=50
----------after advice----------
mut
afterReturning:50
----------after afterReturning advice----------

****************************** div ******************************
----------before advice----------
div
----------after advice----------
div
Exception:/ by zero
----------Exception advice----------

项目下载

请到码云:下载

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值