从动态代理到Spring AOP(中) (2)

  例子中,有2个增强器。那我们看下源码是如何拿到所有的增强器的,看这个方法:org.springframework.aop.aspectj.annotation.BeanFactoryAspectJAdvisorsBuilder#buildAspectJAdvisors

1 /** 2 * Look for AspectJ-annotated aspect beans in the current bean factory, 3 * and return to a list of Spring AOP Advisors representing them. 4 * <p>Creates a Spring Advisor for each AspectJ advice method. 5 * @return the list of {@link org.springframework.aop.Advisor} beans 6 * @see #isEligibleBean 7 */ 8 public List<Advisor> buildAspectJAdvisors() { 9 List<String> aspectNames = null; 10 synchronized (this) { 11 aspectNames = this.aspectBeanNames; 12 if (aspectNames == null) { 13 List<Advisor> advisors = new LinkedList<Advisor>(); 14 aspectNames = new LinkedList<String>(); 15 // 获取容器内所有的beanName 16 String[] beanNames = 17 BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, Object.class, true, false); 18 // for循环,找出增强方法 19 for (String beanName : beanNames) { 20 // 过滤不合法的bean,子类实现 21 if (!isEligibleBean(beanName)) { 22 continue; 23 } 24 // We must be careful not to instantiate beans eagerly as in this 25 // case they would be cached by the Spring container but would not 26 // have been weaved 27 // 获取到bean类型 28 Class beanType = this.beanFactory.getType(beanName); 29 if (beanType == null) { 30 continue; 31 } 32 // 如果存在@AspectJ注解 33 if (this.advisorFactory.isAspect(beanType)) { 34 // 加入list 35 aspectNames.add(beanName); 36 AspectMetadata amd = new AspectMetadata(beanType, beanName); 37 if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { 38 MetadataAwareAspectInstanceFactory factory = 39 new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); 40 41 // 解析获取增强方法 42 List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); 43 if (this.beanFactory.isSingleton(beanName)) { 44 // 放入缓存 45 this.advisorsCache.put(beanName, classAdvisors); 46 } 47 else { 48 this.aspectFactoryCache.put(beanName, factory); 49 } 50 advisors.addAll(classAdvisors); 51 } 52 else { 53 // Per target or per this. 54 if (this.beanFactory.isSingleton(beanName)) { 55 throw new IllegalArgumentException("Bean with name '" + beanName + 56 "' is a singleton, but aspect instantiation model is not singleton"); 57 } 58 MetadataAwareAspectInstanceFactory factory = 59 new PrototypeAspectInstanceFactory(this.beanFactory, beanName); 60 this.aspectFactoryCache.put(beanName, factory); 61 advisors.addAll(this.advisorFactory.getAdvisors(factory)); 62 } 63 } 64 } 65 this.aspectBeanNames = aspectNames; 66 return advisors; 67 } 68 } 69 if (aspectNames.isEmpty()) { 70 return Collections.EMPTY_LIST; 71 } 72 List<Advisor> advisors = new LinkedList<Advisor>(); 73 for (String aspectName : aspectNames) { 74 // 从缓冲拿出增强器 75 List<Advisor> cachedAdvisors = this.advisorsCache.get(aspectName); 76 if (cachedAdvisors != null) { 77 advisors.addAll(cachedAdvisors); 78 } 79 else { 80 MetadataAwareAspectInstanceFactory factory = this.aspectFactoryCache.get(aspectName); 81 advisors.addAll(this.advisorFactory.getAdvisors(factory)); 82 } 83 } 84 return advisors; 85 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zydszw.html