MethodInterceptor;方法调用拦截器;使用:打印操作日志,阿里sentinal接入用于服务降级

Interface MethodInterceptor

  • org.aopalliance.intercept.MethodInterceptor

    • invoke(MethodInvocation invocation)
  • 这是一个函数接口,因此可以用作lambda表达式或方法引用的赋值目标。

  • 拦截在到达目标的过程中对接口的调用。它们嵌套在目标(target)的“顶部”。

  • 用户应该实现invoke(MethodInvocation)方法来修改原始行为。例如:以下的类实现了一个追踪拦截器(追踪所有对拦截方法的调用)

invoke(MethodInvocation invocation)

  • 实现此方法以在调用的方法之前和之后执行额外的处理。
  • invocation:方法调用连接点Joinpoint
  • 简洁的实现当然希望调用Joinpoint.proceed()。
  • 调用Joinpoint.proceed()的结果;可能被拦截机拦截

使用场景

  1. 追踪拦截器
    场景一打印操作日志,可扩展:‘添加调用链’
class TracingInterceptor implements MethodInterceptor {
   
   Object invoke(MethodInvocation i) throws Throwable {
   
     System.out.println("method "+i.getMethod()+" is called on "+
                        i.getThis()+" with args "+i.getArguments());
     Object ret=i.proceed()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值