引言 源码分析入口为:AbstractAdvisorAutoProxyCreator的getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, @Nullable TargetSource targetSource)方法:
1 2 3 4 5 6 7 8 9 10 11 12 @Override @Nullable protected Object[] getAdvicesAndAdvisorsForBean( Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) { List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName); if (advisors.isEmpty()) { return DO_NOT_PROXY; } return advisors.toArray(); }
findEligibleAdvisors流程入口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 protected List<Advisor> findEligibleAdvisors (Class<?> beanClass, String beanName) { List<Advisor> candidateAdvisors = findCandidateAdvisors(); List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName); extendAdvisors(eligibleAdvisors); if (!eligibleAdvisors.isEmpty()) { eligibleAdvisors = sortAdvisors(eligibleAdvisors); } return eligibleAdvisors; }
这里面有两个重要的方法:
findCandidateAdvisors() 获取所有的Advisor。
findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName) 获取当前bean所匹配的Advisor。
1、findCandidateAdvisors 这个方法由子类AnnotationAwareAspectJAutoProxyCreator实现:
1 2 3 4 5 6 7 8 9 10 11 12 @Override protected List<Advisor> findCandidateAdvisors () { List<Advisor> advisors = super .findCandidateAdvisors(); if (this .aspectJAdvisorsBuilder != null ) { advisors.addAll(this .aspectJAdvisorsBuilder.buildAspectJAdvisors()); } return advisors; }
首先会调用父类AbstractAdvisorAutoProxyCreator中的findCandidateAdvisors方法, 从当前IOC容器中获取所有Advisor:
1 2 3 4 5 6 7 8 9 10 protected List<Advisor> findCandidateAdvisors () { Assert.state(this .advisorRetrievalHelper != null , "No BeanFactoryAdvisorRetrievalHelper available" ); return this .advisorRetrievalHelper.findAdvisorBeans(); }
BeanFactoryAdvisorRetrievalHelper中的findAdvisorBeans:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 public List<Advisor> findAdvisorBeans () { String[] advisorNames = this .cachedAdvisorBeanNames; if (advisorNames == null ) { advisorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this .beanFactory, Advisor.class, true , false ); this .cachedAdvisorBeanNames = advisorNames; } if (advisorNames.length == 0 ) { return new ArrayList <>(); } List<Advisor> advisors = new ArrayList <>(); for (String name : advisorNames) { if (isEligibleBean(name)) { if (this .beanFactory.isCurrentlyInCreation(name)) { if (logger.isTraceEnabled()) { logger.trace("Skipping currently created advisor '" + name + "'" ); } } else { try { advisors.add(this .beanFactory.getBean(name, Advisor.class)); } catch (BeanCreationException ex) { Throwable rootCause = ex.getMostSpecificCause(); if (rootCause instanceof BeanCurrentlyInCreationException) { BeanCreationException bce = (BeanCreationException) rootCause; String bceBeanName = bce.getBeanName(); if (bceBeanName != null && this .beanFactory.isCurrentlyInCreation(bceBeanName)) { if (logger.isTraceEnabled()) { logger.trace("Skipping advisor '" + name + "' with dependency on currently created bean: " + ex.getMessage()); } continue ; } } throw ex; } } } } return advisors; }
之后获取所有被@Aspect注解修饰的切面: aspectJAdvisorsBuilder.buildAspectJAdvisors() BeanFactoryAspectJAdvisorsBuilder:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 public List<Advisor> buildAspectJAdvisors () { List<String> aspectNames = this .aspectBeanNames; if (aspectNames == null ) { synchronized (this ) { aspectNames = this .aspectBeanNames; if (aspectNames == null ) { List<Advisor> advisors = new ArrayList <>(); aspectNames = new ArrayList <>(); String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this .beanFactory, Object.class, true , false ); for (String beanName : beanNames) { if (!isEligibleBean(beanName)) { continue ; } Class<?> beanType = this .beanFactory.getType(beanName); if (beanType == null ) { continue ; } if (this .advisorFactory.isAspect(beanType)) { aspectNames.add(beanName); AspectMetadata amd = new AspectMetadata (beanType, beanName); if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { MetadataAwareAspectInstanceFactory factory = new BeanFactoryAspectInstanceFactory (this .beanFactory, beanName); List<Advisor> classAdvisors = this .advisorFactory.getAdvisors(factory); if (this .beanFactory.isSingleton(beanName)) { this .advisorsCache.put(beanName, classAdvisors); } else { this .aspectFactoryCache.put(beanName, factory); } advisors.addAll(classAdvisors); } else { if (this .beanFactory.isSingleton(beanName)) { throw new IllegalArgumentException ("Bean with name '" + beanName + "' is a singleton, but aspect instantiation model is not singleton" ); } MetadataAwareAspectInstanceFactory factory = new PrototypeAspectInstanceFactory (this .beanFactory, beanName); this .aspectFactoryCache.put(beanName, factory); advisors.addAll(this .advisorFactory.getAdvisors(factory)); } } } this .aspectBeanNames = aspectNames; return advisors; } } } if (aspectNames.isEmpty()) { return Collections.emptyList(); } List<Advisor> advisors = new ArrayList <>(); for (String aspectName : aspectNames) { List<Advisor> cachedAdvisors = this .advisorsCache.get(aspectName); if (cachedAdvisors != null ) { advisors.addAll(cachedAdvisors); } else { MetadataAwareAspectInstanceFactory factory = this .aspectFactoryCache.get(aspectName); advisors.addAll(this .advisorFactory.getAdvisors(factory)); } } return advisors; }
这里有个重要的方法:
1 2 List<Advisor> classAdvisors = this .advisorFactory.getAdvisors(factory);
比较复杂,所以打算后面分析,本篇把流程走完。
2、findAdvisorsThatCanApply 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 protected List<Advisor> findAdvisorsThatCanApply ( List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName) { ProxyCreationContext.setCurrentProxiedBeanName(beanName); try { return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass); } finally { ProxyCreationContext.setCurrentProxiedBeanName(null ); } }
这里面的AopUtils的方法通过ClassFilter的 matches(Class> clazz)方法,和MethodMatcher的matches(Method method, Class> targetClass)方法过滤出当前bean实例使用的Advisors。 这个方法也很重要,难度也比较大,所以也拉出来单独分析。
经过上面的分析,还剩下两块主要的部分没有分析:
通过注解@AspectJ获取Advisiors 通过AspectJAdvisorFactory与ListableBeanFactory获取Advisor列表,为啥是列表?因为一个切面中会有多个Advice, 而Advisor与Advice是一对一的关系,所以一个切面会有多个Advisor:1 2 List<Advisor> classAdvisors = this .advisorFactory.getAdvisors(factory);
ClassFilter与MethodMatcher过滤出当前bean实例的使用Advisors
篇幅太长,下面分两篇文章分析。