Spring--官方文档部分翻译(第六章 AOP)

6.1 切点API

6.11 概念

  • 核心接口:org.springframework.aop.Pointcut
public interface Pointcut {
    ClassFilter getClassFilter();
    MethodMatcher getMethodMatcher();
}
  • 两个子对象都有matches方法,用于测试切点是否匹配特定的类或方法。这种测试在AOP代理生成时完成以避免在每次方法调用时测试。
  • 如果一个方法的2参数matches和isRuntime都为true,那么每次方法调用时3参数matches都会被调用,这使得在方法调用时可以核查参数信息。
  • 大部分MethodMatcher的实现为static,isRuntime始终返回false,则其3参数matches永远不会触发。
  • 尽量让切点定义为static,以允许spring在AOP代理生成时缓存切点评估的结果。
public interface MethodMatcher {
    boolean matches(Method m, Class targetClass);
    boolean isRuntime();
    boolean matches(Method m, Class targetClass, Object[] args);
}

6.1.4 切点实现

6.2 Advice API

6.2.1 Advice生命周期

  • 每个Advice是一个bean。An advice instance can be shared across all advised objects or be unique to each advised object. This corresponds to per-class or per-instance advice.
  • Per-class advice使用更广泛。

6.2.1 Advice Type

  • 拦截器:最基础的Advice Type是环绕Advice的拦截器。
  • 需要实现MethodInterceptor接口(invoke方法返回调用的结果)
public interface MethodInterceptor extends Interceptor {
    Object invoke(MethodInvocation invocation) throws Throwable;
}

public class DebugInterceptor implements MethodInterceptor {
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("Before: invocation=[" + invocation + "]");
        Object rval = invocation.proceed();
        System.out.println("Invocation returned");
        return rval;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值