接上一回,講到了getAdvicesAndAdvisorsForBean方法,該方法的目的是獲取並生成Advisor Bean。其中包含了掃描經過@Aspect註解配置且與Bean方法的匹配的Advice,也是本章主要講的內容java
/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java @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(); } /** * Find all eligible Advisors for auto-proxying this class. * @param beanClass the clazz to find advisors for * @param beanName the name of the currently proxied bean * @return the empty List, not {@code null}, * if there are no pointcuts or interceptors * @see #findCandidateAdvisors * @see #sortAdvisors * @see #extendAdvisors */ 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; } ....
關注findEligibleAdvisors方法能夠清楚地知道這裏主要分四步:spring
什麼是Advisor? 首先,Advice是加強方法,即@Around, @Before等註解修飾的方法。而Advisor則是在Advice之上再包了一層。例如PointcutAdvisor則包有Advice和Pointcut
下面接着看findCandidateAdvisors和findAdvisorsThatCanApply緩存
/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java .... /** * Find all candidate Advisors to use in auto-proxying. * @return the List of candidate Advisors */ protected List<Advisor> findCandidateAdvisors() { Assert.state(this.advisorRetrievalHelper != null, "No BeanFactoryAdvisorRetrievalHelper available"); return this.advisorRetrievalHelper.findAdvisorBeans(); } .... /org/springframework/aop/aspectj/annotation/AnnotationAwareAspectJAutoProxyCreator.java .... @Override protected List<Advisor> findCandidateAdvisors() { // Add all the Spring advisors found according to superclass rules. List<Advisor> advisors = super.findCandidateAdvisors(); // Build Advisors for all AspectJ aspects in the bean factory. if (this.aspectJAdvisorsBuilder != null) { advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors()); } return advisors; } ....
這裏findCandidateAdvisors在AbstractAdvisorAutoProxyCreator中有實現,同時被AnnotationAwareAspectJAutoProxyCreator重寫了。不過能夠看到重寫的方法中先調用了super.findCandidateAdvisor,所以兩個方法的代碼都被執行了。app
/org/springframework/aop/aspectj/annotation/BeanFactoryAspectJAdvisorsBuilder.java .... /** * Look for AspectJ-annotated aspect beans in the current bean factory, * and return to a list of Spring AOP Advisors representing them. * <p>Creates a Spring Advisor for each AspectJ advice method. * @return the list of {@link org.springframework.aop.Advisor} beans * @see #isEligibleBean */ public List<Advisor> buildAspectJAdvisors() { // aspectBeanNames,緩存 // tips: aspectBeanNames由volatile修飾 // volatile: 保證變量可見性,指令不可重排 List<String> aspectNames = this.aspectBeanNames; // 如緩存存在,則跳過初始化步驟 if (aspectNames == null) { // synchronized, 同步鎖 // 鎖住當前實例,裏面的內容同一時間只會被一個線程執行 synchronized (this) { // 拿到鎖後再次獲取緩存,避免重複初始化 aspectNames = this.aspectBeanNames; if (aspectNames == null) { List<Advisor> advisors = new ArrayList<>(); aspectNames = new ArrayList<>(); // 獲取原始類爲Object的bean,即全部bean String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this.beanFactory, Object.class, true, false); for (String beanName : beanNames) { // 是不是合適的bean if (!isEligibleBean(beanName)) { continue; } // We must be careful not to instantiate beans eagerly as in this case they // would be cached by the Spring container but would not have been weaved. Class<?> beanType = this.beanFactory.getType(beanName); if (beanType == null) { continue; } // isAspect 是否爲@Aspect註解修飾的Bean // 是否是很親切方法,終於讀到它了。這是咱們在第二章一開始就提到的方法。 if (this.advisorFactory.isAspect(beanType)) { aspectNames.add(beanName); // 由beanType,beanName組合Metadata,包含了建立Advisor須要的內容 AspectMetadata amd = new AspectMetadata(beanType, beanName); // PerClauseKind.SINGLETON 單例模式 // 由@Aspect中的value參數配置,這個參數Bean的scope 有點相似,用來配置生命 週期,默認都爲單例。可配爲"每..."的模式 if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { // 生成實例工廠類 MetadataAwareAspectInstanceFactory factory = new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); // 生成Advisors實例 List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); // 若是Aspect Bean是單例,則緩存到advisorsCache if (this.beanFactory.isSingleton(beanName)) { this.advisorsCache.put(beanName, classAdvisors); } // 若是不是,則將工廠緩存到aspectFactoryCache else { this.aspectFactoryCache.put(beanName, factory); } advisors.addAll(classAdvisors); } else { // Per target or per this. // bean爲單例,@Aspect也要配置爲單例 if (this.beanFactory.isSingleton(beanName)) { throw new IllegalArgumentException("Bean with name '" + beanName + "' is a singleton, but aspect instantiation model is not singleton"); } // 跟前一個分支同樣,生成Advisors實例,而後將工廠緩存到aspectFactoryCache 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) { // 經過aspectName(beanName)獲取advisors緩存 List<Advisor> cachedAdvisors = this.advisorsCache.get(aspectName); // 如已存在,則加載advisorsCache if (cachedAdvisors != null) { advisors.addAll(cachedAdvisors); } // 如不存在,則加載factoryCache,再從工廠生成advisors,與上面初始時候的兩個分支對應 // ps:這裏並無作factory的空判斷... else { MetadataAwareAspectInstanceFactory factory = this.aspectFactoryCache.get(aspectName); advisors.addAll(this.advisorFactory.getAdvisors(factory)); } } return advisors; } ....
因爲這段代碼比較長,我將過程註釋在代碼中。其中英文爲源碼註釋。ide
那麼,以上即是經過beanName掃描@Aspect配置並生成Advisor的過程了。其中this.advisorFactory.getAdvisors(factory)是生成Advisor類的具體內容。深挖的話還能再寫一篇文章,這裏就不細說了。有興趣的能夠自行閱讀。ui
如今咱們得到了全部的候選Advisor,那麼找出和當前Bean匹配的Advisor呢?this
/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java .... /** * Search the given candidate Advisors to find all Advisors that * can apply to the specified bean. * @param candidateAdvisors the candidate Advisors * @param beanClass the target's bean class * @param beanName the target's bean name * @return the List of applicable Advisors * @see ProxyCreationContext#getCurrentProxiedBeanName() */ protected List<Advisor> findAdvisorsThatCanApply( List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName) { ProxyCreationContext.setCurrentProxiedBeanName(beanName); try { return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass); } finally { ProxyCreationContext.setCurrentProxiedBeanName(null); } } ....
一步一步往下探lua
/org/springframework/aop/support/AopUtils.java .... /** * Determine the sublist of the {@code candidateAdvisors} list * that is applicable to the given class. * @param candidateAdvisors the Advisors to evaluate * @param clazz the target class * @return sublist of Advisors that can apply to an object of the given class * (may be the incoming List as-is) */ public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz) { if (candidateAdvisors.isEmpty()) { return candidateAdvisors; } List<Advisor> eligibleAdvisors = new ArrayList<>(); 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) { // already processed continue; } if (canApply(candidate, clazz, hasIntroductions)) { eligibleAdvisors.add(candidate); } } return eligibleAdvisors; } .... /** * Can the given advisor apply at all on the given class? * This is an important test as it can be used to optimize * out a advisor for a class. * @param advisor the advisor to check * @param targetClass class we're testing * @return whether the pointcut can apply on any method */ public static boolean canApply(Advisor advisor, Class<?> targetClass) { return canApply(advisor, targetClass, false); } /** * Can the given advisor apply at all on the given class? * <p>This is an important test as it can be used to optimize out a advisor for a class. * This version also takes into account introductions (for IntroductionAwareMethodMatchers). * @param advisor the advisor to check * @param targetClass class we're testing * @param hasIntroductions whether or not the advisor chain for this bean includes * any introductions * @return whether the pointcut can apply on any method */ 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; } } ....
最後定位到canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions)方法線程
/org/springframework/aop/support/AopUtils.java /** * Can the given pointcut apply at all on the given class? * <p>This is an important test as it can be used to optimize * out a pointcut for a class. * @param pc the static or dynamic pointcut to check * @param targetClass the class to test * @param hasIntroductions whether or not the advisor chain * for this bean includes any introductions * @return whether the pointcut can apply on any method */ 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(); if (methodMatcher == MethodMatcher.TRUE) { // No need to iterate the methods if we're matching any method anyway... return true; } IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null; if (methodMatcher instanceof IntroductionAwareMethodMatcher) { introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher; } Set<Class<?>> classes = new LinkedHashSet<>(); if (!Proxy.isProxyClass(targetClass)) { classes.add(ClassUtils.getUserClass(targetClass)); } classes.addAll(ClassUtils.getAllInterfacesForClassAsSet(targetClass)); for (Class<?> clazz : classes) { Method[] methods = ReflectionUtils.getAllDeclaredMethods(clazz); for (Method method : methods) { if (introductionAwareMethodMatcher != null ? introductionAwareMethodMatcher.matches(method, targetClass, hasIntroductions) : methodMatcher.matches(method, targetClass)) { return true; } } } return false; }
能夠看出判斷是不是該bean合適的advisor,是經過advisor.getPointcut().getClassFilter().matches(targetClass)方法來判斷的。匹配完class之後下面還有MethodMatcher來匹配method。回想咱們在配置pointcut的時候不單單有class的規則,也有method的規則。code
固然,再深刻matches方法進去的話就是pointcut的匹配語法實現了。有興趣的能夠自行閱讀。
讀到這兒,Spring AOP如何掃描@Aspect配置,生成Advisor類,並匹配對應的Bean整個流程已經很清楚了。這裏再總結一下: