Spring环绕通知
/**
* @throws Throwable
* @Around 环绕通知是spring中最强大的通知方法,其本身就是一个动态代理
*
* 通知方法
* try{
* @Before
* 目标方法
* @AfterReturning
* }catch(e){
* @AfterThrowing
* }finally{
* @After
* }
*
* 而四合一就是环绕通知,环绕通知中有一个参数;ProceedingJoinPoint
*/
@Around("execution(public int com.atguigu.impl.MyMathCalculator.*(int, int))")
public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
//获取方法参数,
Object[] args = joinPoint.getArgs();
Object proceed = null;
try {
System.out.println("环绕前置通知在写这里");
//利用反射调用目标方法,它就是目标方法被调用
proceed = joinPoint.proceed(args);
System.out.println("环绕返回通知在这里,返回值是"+proceed);
} catch (Exception e) {
System.out.println("环绕异常通知在这里");
e.printStackTrace();
}finally {
System.out.println("环绕后置通知在这里");
}
//返回调用后的返回值一定返回出去
return proceed;
}
环绕通知是优于普通通知执行的:
[普通前置]
{
环绕前置
try{
环绕执行目标方法
环绕返回
}catch
出现异常
}finally{
环绕后置
}
[普通后置]
[普通方法返回/方法异常]
执行顺序:【环绕前置–>普通前置】–>环绕执行目标方法–> 环绕返回/异常–> 环绕后置–>普通后置–>普通方法返回/异常