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

监听到了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方法

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值