切面的用法,获取切点的参数,以及返回值

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_20009015/article/details/84581193

监听到了impl下面的所有的方法

ps:注意

由@Before注解定义的方法会在 execution() 表达式内的方法被调用之前执行

由@After注解定义的方法会在 execution()表达式内的方法被调用之后执行,无论方法执行成功与否

由@AfterReturning注解定义的方法会在 execution()表达式内的方法被调用之后并成功返回结果后执行,若抛出异常AfterReturning不会执行

由@AfterThrowing注解定义的方法会在 execution()表达式内的方法抛出异常后执行

由@Around注解定义的方法包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为

execution表达式

如:@AfterReturning("execution(* com.tangcy.npcmeeting.controller.OperateController.save(..))")

* 代表匹配方法的所有修饰符和所有返回值,经测式,方法修饰符若为private则不行,会出错 500,这里也可以这样写

@AfterReturning("execution(public void com.tangcy.npcmeeting.controller.OperateController.save(..))")

在 * 的位置换成你的方法修饰符跟返回值类型即可

.. 代表匹配方法的所有参数,无论该方法有多少个参数,是什么类型都匹配,若你的方法行参类型为String 也可以这样写

在..的地方换成你的参数类型即可,可写多个参数类型

@AfterReturning("execution(* com.tangcy.npcmeeting.controller.OperateController.save(String))")

com.tangcy.npcmeeting.controller 为方法的包路径/名

ProceedingJoinPoint 只能用于around

https://blog.csdn.net/zhou870498/article/details/80071402

可以用切面来监听捕获切点的异常,需要joinPoint来获取到参数和异常信息

@Around("execution(* com.neusoft.service.impl..*.*(..))")//切点表达式以及通知类型

public Object around(ProceedingJoinPoint joinPoint){//通过joinPoint对象获取参数以及其他对象信息

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

Object result = null;

try {

result = joinPoint.proceed();

} catch (Throwable e) {

if(ResultCode.contains(e.getMessage())){

return ResultUtils.setError(ResultUtils.getResultCodeByName(e.getMessage()));

}

Object[] args = joinPoint.getArgs();

StringBuffer stringBuffer = new StringBuffer();

for (Object ob :args){

stringBuffer.append(ob);

stringBuffer.append(",");

}

logger.error("Method:"+MethodName+",Params:"+stringBuffer.toString()+"error:"+e.getMessage());

return ResultUtils.setError(ResultCode.SYSTEM_ERROR);

}

return result;

}

---------------------

注意!!监听的时候,不要去监听controller 而是去监听controller所调用的service方法

切点和获取切点方法处的参数

猜你喜欢

转载自blog.csdn.net/qq_20009015/article/details/84581193