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----------
项目下载
请到码云:下载