Java 为什么Spring框架中的MethodMatcher接口将targetClass作为参数,而切入点已经定义了一个类过滤器?
中的接口包括两种方法:Java 为什么Spring框架中的MethodMatcher接口将targetClass作为参数,而切入点已经定义了一个类过滤器?,java,spring,spring-aop,Java,Spring,Spring Aop,中的接口包括两种方法: public interface Pointcut { /** * Return the ClassFilter for this pointcut. * @return the ClassFilter (never {@code null}) */ ClassFilter getClassFilter(); /** * Return the MethodMatcher for this pointcut
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;
}
接口已经声明,其唯一目的是确定类是否可以通过过滤器:
/**
* 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;
}
/**
*限制切入点或引入点匹配的筛选器
*一组给定的目标类。
*
*可以用作{@link Pointcut}的一部分或整个
*以{@link IntroductionAdvisor}为目标。
*
*@作者罗德·约翰逊
*@见切入点
*@see MethodMatcher
*/
公共接口类过滤器{
/**
*切入点应该应用于给定的接口或目标类吗?
*@param clazz候选目标类
*@return建议是否应用于给定的目标类
*/
布尔匹配(类clazz);
/**
*匹配所有类的类筛选器的规范实例。
*/
ClassFilter TRUE=TrueClassFilter.INSTANCE;
}
我不明白的是,为什么接口中的方法再次检查类资格?为什么此接口的方法具有targetClass参数?
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);
/**
* 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;
}
公共接口MethodMatcher{
/**
*执行静态检查给定方法是否匹配。如果
*返回{@code false}或如果{@link#isRuntime()}方法
*返回{@code false},没有运行时检查(即没有。
*将进行{@link#匹配(java.lang.reflect.Method,Class,Object[])}调用。
*@param方法候选方法
*@param targetClass目标类(可能是{@code null},在这种情况下
*候选类必须作为方法的声明类)
*@return此方法是否静态匹配
*/
布尔匹配(方法,类targetClass);
/**
*检查此方法是否存在运行时(动态)匹配,
*一定是静态匹配的。
*仅当2-arg与返回的方法匹配时,才会调用此方法
*给定方法和目标类的{@code true},如果
*{@link#isRuntime()}方法返回{@code true}。调用
*在建议可能运行之前,在任何
*建议链中较早的建议已运行。
*@param方法候选方法
*@param targetClass目标类(可能是{@code null},在这种情况下
*候选类必须作为方法的声明类)
*@param args方法的参数
*@return是否存在运行时匹配
*@see MethodMatcher#matches(方法,类)
*/
布尔匹配(方法、类targetClass、对象…参数);
/**
*匹配所有方法的规范实例。
*/
MethodMatcher TRUE=TrueMethodMatcher.INSTANCE;
}
在MethodMatcher.matches(方法,类targetClass)
中指定的targetClass
不用于检查目标调用类的合格性
它用于查找适用于给定方法(指定为参数)的目标类的最具体的目标方法。它还解决了与的问题
下面是来自org.springframework.aop.aspectj.AspectJExpressionPointcut
类的matches
方法示例
public boolean matches(Method method, Class<?> targetClass, boolean beanHasIntroductions) {
this.checkReadyToMatch();
Method targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
ShadowMatch shadowMatch = this.getShadowMatch(targetMethod, method);
...
}
公共布尔匹配(方法、类targetClass、布尔BeanHasinProductions){
此参数为.checkReadyToMatch();
方法targetMethod=AopUtils.getMostSpecificMethod(方法,targetClass);
ShadowMatch ShadowMatch=this.getShadowMatch(targetMethod,method);
...
}
这是来自
给定一个方法(可能来自接口)和使用的目标类
在当前AOP调用中,找到相应的目标方法(如果有)。例如,方法可能是IFoo.bar()
,目标类可能是DefaultFoo
。在这种情况下,方法可能是DefaultFoo.bar()
。这样可以找到该方法上的属性
注意:与org.springframework.util.ClassUtils#getmostsspecificmethod
相比,该方法解析Java 5桥接方法,以便从原始方法定义中检索属性