返回介绍

7.3.2 寻找匹配的增强器

发布于 2025-04-22 22:09:14 字数 3521 浏览 0 评论 0 收藏 0

前面的函数中已经完成了所有增强器的解析,但是对于所有增强器来讲,并不一定都适用于当前的 Bean,还要挑取出适合的增强器,也就是满足我们配置的通配符的增强器。具体实现在 findAdvisorsThatCanApply 中。

protected List<Advisor> findAdvisorsThatCanApply(

List<Advisor> candidateAdvisors, Class beanClass, String beanName) {

 ProxyCreationContext.setCurrentProxiedBeanName(beanName);

  try {

  //过滤已经得到的 advisors

   return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);

 }

  finally {

  ProxyCreationContext.setCurrentProxiedBeanName(null);

 }

}

继续看 findAdvisorsThatCanApply:

public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors,

Class<?> clazz) {

  if (candidateAdvisors.isEmpty()) {

   return candidateAdvisors;

 }

  List<Advisor> eligibleAdvisors = new LinkedList<Advisor>();

 //首先处理引介增强

  for (Advisor candidate : candidateAdvisors) {

   if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {

   eligibleAdvisors.add(candidate);

  }

 }

  boolean hasIntroductions = !eligibleAdvisors.isEmpty();

  for (Advisor candidate : candidateAdvisors) {

  //引介增强已经处理

   if (candidate instanceof IntroductionAdvisor) {

   continue;

  }

  //对于普通 bean 的处理

   if (canApply(candidate, clazz, hasIntroductions)) {

   eligibleAdvisors.add(candidate);

  }

 }

  return eligibleAdvisors;

}

findAdvisorsThatCanApply 函数的主要功能是寻找所有增强器中适用于当前 class 的增强器。引介增强与普通的增强是处理不一样的,所以分开处理。而对于真正的匹配在 canApply 中实现。

public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean

hasIntroductions) {

  if (advisor instanceof IntroductionAdvisor) {

   return ((IntroductionAdvisor) advisor).getClassFilter().matches (targetClass);

  }else if (advisor instanceof PointcutAdvisor) {

   PointcutAdvisor pca = (PointcutAdvisor) advisor;

   return canApply(pca.getPointcut(), targetClass, hasIntroductions);

  }else {

   // It doesn't have a pointcut so we assume it applies.

   return true;

 }

}

public static boolean canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions) {

  Assert.notNull(pc, "Pointcut must not be null");

  if (!pc.getClassFilter().matches(targetClass)) {

   return false;

 }

  MethodMatcher methodMatcher = pc.getMethodMatcher();

  IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;

  if (methodMatcher instanceof IntroductionAwareMethodMatcher) {

   introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;

 }

  Set<Class> classes = new HashSet<Class>(ClassUtils.getAllInterfacesForClassAsSet

 (targetClass));

 classes.add(targetClass);

  //classes:[interface test.IITestBean, class test.TestBean]

  for (Class<?> clazz : classes) {

   Method[] methods = clazz.getMethods();

   for (Method method : methods) {

    if ((introductionAwareMethodMatcher != null &&

     introductionAwareMethodMatcher.matches(method, targetClass,

     hasIntroductions)) ||

      methodMatcher.matches(method, targetClass)) {

     return true;

   }

  }

 }

  return false;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。