AOP-Pointcut-笔记
一、Pointcut
这是切点的抽象。一个切点由一个的类过滤器和一个方法匹配器组成。
将整个代码贴上来
/** * Core Spring pointcut abstraction. * * <p>A pointcut is composed of a {@link ClassFilter} and a {@link MethodMatcher}. * Both these basic terms and a Pointcut itself can be combined to build up combinations * (e.g. through {@link org.springframework.aop.support.ComposablePointcut}). * * @author Rod Johnson * @see ClassFilter * @see MethodMatcher * @see org.springframework.aop.support.Pointcuts * @see org.springframework.aop.support.ClassFilters * @see org.springframework.aop.support.MethodMatchers */ public interface Pointcut { /** * Return the ClassFilter for this pointcut. * @return the ClassFilter (never {@code null}) *///返回一个类过滤器 ClassFilter getClassFilter(); /** * Return the MethodMatcher for this pointcut. * @return the MethodMatcher (never {@code null}) *///返回一个方法匹配器 MethodMatcher getMethodMatcher(); /** * Canonical Pointcut instance that always matches. */ Pointcut TRUE = TruePointcut.INSTANCE; }
二、ClassFilter
一个过滤器可以使一个切点或一个引入与给定的目标类的集合相匹配。可以用作一个Pointcut的一部分,或者是一个引入IntroductionAdvisor的一部分。也就是说可以作为的Pointcut的属性,也可以作为IntroductionAdvisor的属性。
这个接口代码较少,所以将整个代码贴上来:
/** * Filter that restricts matching of a pointcut or introduction to * a given set of target classes. * * <p>Can be used as part of a {@link Pointcut} or for the entire * targeting of an {@link IntroductionAdvisor}. * * @author Rod Johnson * @see Pointcut * @see MethodMatcher */ public interface ClassFilter { /** * Should the pointcut apply to the given interface or target class? * @param clazz the candidate target class 给定的目标类 * @return whether the advice should apply to the given target class *///判断一个切点是否适用于一个给定的目标类或目标接口。 boolean matches(Class<?> clazz); /** * Canonical instance of a ClassFilter that matches all classes. */ ClassFilter TRUE = TrueClassFilter.INSTANCE; }
三、MethodMatcher
检查一个目标方法与通知是否相符合。
如果它的实现类的isRuntime()方法返回false。
这个接口的代码:
/** * Part of a {@link Pointcut}: Checks whether the target method is eligible for advice. * 一、检查一个目标方法与通知是否相匹配。 * <p>A MethodMatcher may be evaluated <b>statically</b> or at <b>runtime</b> (dynamically). * Static matching involves method and (possibly) method attributes. Dynamic matching * also makes arguments for a particular call available, and any effects of running * previous advice applying to the joinpoint. * * <p>If an implementation returns {@code false} from its {@link #isRuntime()} * method, evaluation can be performed statically, and the result will be the same * for all invocations of this method, whatever their arguments. This means that * if the {@link #isRuntime()} method returns {@code false}, the 3-arg * {@link #matches(java.lang.reflect.Method, Class, Object[])} method will never be invoked. * * <p>If an implementation returns {@code true} from its 2-arg * {@link #matches(java.lang.reflect.Method, Class)} method and its {@link #isRuntime()} method * returns {@code true}, the 3-arg {@link #matches(java.lang.reflect.Method, Class, Object[])} * method will be invoked <i>immediately before each potential execution of the related advice</i>, * to decide whether the advice should run. All previous advice, such as earlier interceptors * in an interceptor chain, will have run, so any state changes they have produced in * parameters or ThreadLocal state will be available at the time of evaluation. * * @author Rod Johnson * @since 11.11.2003 * @see Pointcut * @see ClassFilter */ public interface MethodMatcher { /** * Perform static checking whether the given method matches. If this * returns {@code false} or if the {@link #isRuntime()} method * returns {@code false}, no runtime check (i.e. no. * {@link #matches(java.lang.reflect.Method, Class, Object[])} call) will be made. * @param method the candidate method * @param targetClass the target class (may be {@code null}, in which case * the candidate class must be taken to be the method's declaring class) * @return whether or not this method matches statically */ boolean matches(Method method, Class<?> targetClass); /** * Is this MethodMatcher dynamic, that is, must a final call be made on the * {@link #matches(java.lang.reflect.Method, Class, Object[])} method at * runtime even if the 2-arg matches method returns {@code true}? * <p>Can be invoked when an AOP proxy is created, and need not be invoked * again before each method invocation, * @return whether or not a runtime match via the 3-arg * {@link #matches(java.lang.reflect.Method, Class, Object[])} method * is required if static matching passed */ boolean isRuntime(); /** * Check whether there a runtime (dynamic) match for this method, * which must have matched statically. * <p>This method is invoked only if the 2-arg matches method returns * {@code true} for the given method and target class, and if the * {@link #isRuntime()} method returns {@code true}. Invoked * immediately before potential running of the advice, after any * advice earlier in the advice chain has run. * @param method the candidate method * @param targetClass the target class (may be {@code null}, in which case * the candidate class must be taken to be the method's declaring class) * @param args arguments to the method * @return whether there's a runtime match * @see MethodMatcher#matches(Method, Class) */ boolean matches(Method method, Class<?> targetClass, Object... args); /** * Canonical instance that matches all methods. */ MethodMatcher TRUE = TrueMethodMatcher.INSTANCE; }