java around_关于Aop切面中的@Before @Around等操作顺序的说明

话不多说,直接上代码:

package com.cdms.aop.aspectImpl;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.*;

import org.springframework.stereotype.Component;

import java.util.Arrays;

import java.util.List;

/**

* 创建 by 草帽boy on 2017/4/1.

*/

@Aspect

@Component

public class ICacheAopAction {

@Pointcut("@annotation(com.cdms.aop.ICache)")

private void controllerAspect(){}

@Before("controllerAspect()")

public void Before(JoinPoint joinPoint){

String classname = joinPoint.getTarget().getClass().getSimpleName();

String methodName = joinPoint.getSignature().getName();

List args = Arrays.asList(joinPoint.getArgs());

System.out.println("@before Execute! --class name: " + classname + ", method name: " + methodName + " " + args );

}

@Around("controllerAspect()")

public Object Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

System.out.println("@Around:执行目标方法之前...");

Object obj= proceedingJoinPoint.proceed();

System.out.println("@Around:执行目标方法之后...");

System.out.println("@Around:被织入的目标对象为:" + proceedingJoinPoint.getTarget());

System.out.println( "@Around:原返回值:" + obj + ",这是返回结果的后缀");

return obj;

}

@AfterThrowing("controllerAspect()")

public void AfterThrowing(){

System.out.println("异常通知....");

}

@After("controllerAspect()")

public void After(JoinPoint point){

System.out.println("@After:模拟释放资源...");

System.out.println("@After:目标方法为:" +

point.getSignature().getDeclaringTypeName() +

"." + point.getSignature().getName());

System.out.println("@After:参数为:" + Arrays.toString(point.getArgs()));

System.out.println("@After:被织入的目标对象为:" + point.getTarget());

}

@AfterReturning("controllerAspect()")

public void AfterReturning(JoinPoint point){

System.out.println("@AfterReturning:模拟日志记录功能...");

System.out.println("@AfterReturning:目标方法为:" +

point.getSignature().getDeclaringTypeName() +

"." + point.getSignature().getName());

System.out.println("@AfterReturning:参数为:" +

Arrays.toString(point.getArgs()));

System.out.println("@AfterReturning:返回值为:" );

System.out.println("@AfterReturning:被织入的目标对象为:" + point.getTarget());

}

}

测试的结果是:

fc98d3d3fd30c98b58f554c78ed7fdfc.png

这样就很清楚的看出各种方法是在什么时候调用的啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值