Spring源碼之Aop

本文主要介紹Spring的aop:aspectj-autoproxy/標籤,瞭解spring是如何實現掃描註解進行aop的,主要實現是在 AspectJAutoProxyBeanDefinitionParser的parser方法中,另外這裏php

還須要瞭解一下NamespaceHandler, NamespaceHandlerSupport 和 BeanDefinitionParser 的關係,若是不清楚的能夠看一下Spring源碼之ApplicationContext中的解釋。java

1 Jdk動態代理和CGLIB代理

​ 在講述aop源碼以前,須要先了解一下 Jdk 動態代理和 CGLIB 代理的區別和使用。node

  • Jdk動態代理

    描述

    Jdk動態代理須要目標類至少實現一個接口,在運行時期生成代理類。git

  • CGLIB代理

    描述

    CGLIB代理無需實現接口,經過生成類字節碼實現代理,比反射稍快,不存在性能問題,但CGLIB會繼承目標對象,須要重寫方法,因此目標對象不能爲final類。github

2 示例代碼

2.1 AspetJTest註解

示例是基於註解形式,AspetJTest類爲註解類。正則表達式

package lantao.aop;

import java.lang.annotation.*;

/** * @Auther: lantao * @Date: 2019-05-09 14:01 * @Company: 隨行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AspetJTest {
}
複製代碼

2.2 Aop配置類

Config爲Aop的配置類,Pointcut(切點)配置爲AspetJTest註解,則全部使用@AspetJTest註解的方法都會被代理。spring

package lantao.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/** * @Auther: lantao * @Date: 2019-05-09 14:03 * @Company: 隨行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */
@Aspect
@Component
public class Config {

   @Pointcut("@annotation(AspetJTest)")
   public void serviceAspect() {
   }

   @Around("serviceAspect()")
   public Object Around(ProceedingJoinPoint point) throws Throwable {
      System.out.println("進入Around方法");
      Object proceed = point.proceed();
      System.out.println("退出Around方法");
      return proceed;
   }

   @After("serviceAspect()")
   public void after(){
      System.out.println("進入after方法");
   }

   @Before("serviceAspect()")
   public void before(){
      System.out.println("進入before方法");
   }
}
複製代碼

2.3 註解使用類

TestService真正的業務類,例如輸入插入/刪除等,aop代理實現事物。數據庫

package lantao.aop;

import org.springframework.stereotype.Service;

/** * @Auther: lantao * @Date: 2019-05-09 13:59 * @Company: 隨行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */
@Service
public class TestService {

   @aspetJTest
   public void printInfo() {
      System.out.println("進入了printInfo方法");
   }
}
複製代碼

2.4 測試類

TestAopMain就是測試類。express

package lantao.aop;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;
import java.util.List;

/** * @Auther: lantao * @Date: 2019-05-09 14:06 * @Company: 隨行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */
public class TestAopMain {
   public static void main(String[] args) {
      ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-bean-aop.xml");
      TestService bean = classPathXmlApplicationContext.getBean(TestService.class);
      bean.printInfo();
   }
}
複製代碼

2.5 Aop xml配置類

Xml配置類,在其中有aop:aspectj-autoproxy/標籤,使Aop生效,context:component-scan/開啓註解掃描。數組

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

   <aop:aspectj-autoproxy/>

   <!-- use-default-filters 屬性的默認值爲 true,即便用默認的 Filter 進行包掃描,而默認的 Filter 對標有 @Service,@Controller,@Component和@Repository 的註解的類進行掃描 -->
   <context:component-scan base-package="lantao.aop" use-default-filters="false">
      <!-- 只掃描 base-package 的 controller 註解 還有對應的 exclude-filter 標籤 排除 ; use-default-filters="false" 和 include-filter 一塊兒使用 和 exclude-filter一塊兒回拋異常-->
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
   </context:component-scan>

</beans>
複製代碼

2.6 執行結果

以上代碼的執行結果

進入Around方法
進入before方法
進入了printInfo方法
退出Around方法
進入after方法
複製代碼

在執行TestAopMain類中的main方法時,發現getBean方法返回的並非目標類,而是目標類的代理類

3 XMl標籤解析源碼講解

3.1 解析Aop配置xml

這裏直接從DefaultBeanDefinitionDocumentReader類的doRegisterBeanDefinitions方法開始講解,因前邊都是xml解析的代碼,已經在Spring源碼之XmlBeanFactory中講過了,其中parseBeanDefinitions方法是作標籤解析使用的

DefaultBeanDefinitionDocumentReader . parseBeanDefinitions方法

protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate){
   //驗證xml namespace, BeanDefinitionParserDelegate.BEANS_NAMESPACE_URI
   if (delegate.isDefaultNamespace(root)) {
      NodeList nl = root.getChildNodes();
      for (int i = 0; i < nl.getLength(); i++) {
         Node node = nl.item(i);
         if (node instanceof Element) {
            Element ele = (Element) node;
            if (delegate.isDefaultNamespace(ele)) {
               //對默認標籤處理
               // 這裏只處理 nade namespace 爲 http://www.springframework.org/schema/beans 的標籤
               parseDefaultElement(ele, delegate);
            }
            else {
               //對自定義標籤處理 非namespace 爲 http://www.springframework.org/schema/beans 的標籤 ,會解析 <context:component-scan base-package="lantao.scan"/> 或者自定義 dubbo
               // 或者 aop
               delegate.parseCustomElement(ele);
            }
         }
      }
   }
   else {
      //對自定義標籤處理
      delegate.parseCustomElement(root);
   }
}
複製代碼

這裏直接關注parseCustomElement方法,parseDefaultElement方法處理的是bean標籤。

@Nullable
public BeanDefinition parseCustomElement(Element ele) {
   return parseCustomElement(ele, null);
}

@Nullable
public BeanDefinition parseCustomElement(Element ele, @Nullable BeanDefinition containingBd) {
   // 獲取node的 NameSpaceURI
   String namespaceUri = getNamespaceURI(ele);
   if (namespaceUri == null) {
      return null;
   }
   // 解析自定義標籤 須要在 Meta-inf 文件加 增長 spring.handlers 文件 例如:http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
   // 根據指定的 NameSpaceURI 獲取 NamespaceHandler handler能夠參考spring.handlers文件
   // abstract NamespaceHandlerSupport 實現了 NamespaceHandler 接口,繼而實現了 NamespaceHandler 的兩個個方法(parser,docreate),自定義handler 須要實現 NamespaceHandlerSupport 類
   // 進行 NamespaceHandler 類的 init 方法的 實現, 主要是作註冊 BeanDefinitionParser( registerBeanDefinitionParser ) , 須要自定義解析類 繼承 BeanDefinitionParser 類
   NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
   if (handler == null) {
      error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
      return null;
   }
   // 解析操做
   return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
}
複製代碼

這裏主要的工做就是機械NamespaceHandler,這裏經過NamespaceHandlerResolver的resolve方法來解析各類NamespaceHandler,最後進行調用解析類的parse方法進行解析,接下來看一下resolve方法

@Override
@Nullable
public NamespaceHandler resolve(String namespaceUri) {
   // 這裏獲取的是全部註冊到 handlerMappings 中的 NamespaceHandler ,
   // 就是 resource/META-INF/spring.handler 中的 key就是namespaceUri ,
   // 這些類都繼承了 NamespaceHandlerSupport 實現了init方法 在init方法中進行 BeanDefinitionParse 的註冊
   Map<String, Object> handlerMappings = getHandlerMappings();
   // 經過 namespaceUri 在 handlerMappings 中獲取對應的處理器或者 className 若是是初始化過的就直接返回,反之進行類初始化工做
   Object handlerOrClassName = handlerMappings.get(namespaceUri);
   if (handlerOrClassName == null) {
      return null;
   }
   else if (handlerOrClassName instanceof NamespaceHandler) {
      return (NamespaceHandler) handlerOrClassName;
   }
   else {
      String className = (String) handlerOrClassName;
      try {
         // 實例化
         Class<?> handlerClass = ClassUtils.forName(className, this.classLoader);
         // 判斷實例化的類的超類或者超級接口 是不是 NamespaceHandler
         if (!NamespaceHandler.class.isAssignableFrom(handlerClass)) {
            throw new FatalBeanException("Class [" + className + "] for namespace [" + namespaceUri +
                  "] does not implement the [" + NamespaceHandler.class.getName() + "] interface");
         }
         NamespaceHandler namespaceHandler = (NamespaceHandler) BeanUtils.instantiateClass(handlerClass);
         // 註冊 自定義標籤所對應的 解析策略類 解析策略類都繼承了 BeanDefinitionParser ,好比 ComponentScanBeanDefinitionParser
         namespaceHandler.init();
         // 放入緩存中
         handlerMappings.put(namespaceUri, namespaceHandler);
         return namespaceHandler;
      }
      catch (ClassNotFoundException ex) {
         throw new FatalBeanException("Could not find NamespaceHandler class [" + className +
               "] for namespace [" + namespaceUri + "]", ex);
      }
      catch (LinkageError err) {
         throw new FatalBeanException("Unresolvable class definition for NamespaceHandler class [" +
               className + "] for namespace [" + namespaceUri + "]", err);
      }
   }
}
複製代碼

resolve方法中一共作了兩件事情

1: 調用getHandlerMappings方法解析resources 中的 META-INF/spring.handlers文件,讀取各類處理類

spring.handlers:
http\://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler
複製代碼

2:進行處理類的實例化操做,而後調用處理類init方法,進行解析類(Parser)的註冊,並將實例化的處理類進行緩存處理,以備下次使用。

@Override
public void init() {
   // In 2.0 XSD as well as in 2.1 XSD.
   registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser());
   registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser());
   registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator());
   registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
}
複製代碼

resolve方法理解後在回到主方法(parseDefaultElement)中,在實例化和解析操做後,調用了處理類的parse方法

return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
複製代碼
@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
   // 在 NamespaceHandlerSupport 中的 parser 集合中獲取 BeanDefinitionParser 的實現類 進行 parser
   BeanDefinitionParser parser = findParserForElement(element, parserContext);
   return (parser != null ? parser.parse(element, parserContext) : null);
}
複製代碼

findParserForElement方法中經過標籤(aspectj-autoproxy)進行獲取對應的處理類(AspectJAutoProxyBeanDefinitionParser)處理類的註冊在實例化處理類後調用init方法已經完成, 接下來看一下 解析類的 parse 方法。

@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
   // 註冊 AnnotationAwareAspectJAutoProxyCreator
   AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element);
  // 擴展BeanDefinition 處理 子節點 <aop:include/>
   extendBeanDefinition(element, parserContext);
   return null;
}
複製代碼

註冊 AnnotationAwareAspectJAutoProxyCreator

在解析**(parse)方法中,首先是註冊了AnnotationAwareAspectJAutoProxyCreator類,這個類是處理註解攔截的代理類,而後又擴展了剛剛註冊的AnnotationAwareAspectJAutoProxyCreator**,對xml中aop:aspectj-autoproxy標籤的子節點aop:include/進行了處理。

首先看一下注冊AnnotationAwareAspectJAutoProxyCreator的代碼

public static void registerAspectJAnnotationAutoProxyCreatorIfNecessary( ParserContext parserContext, Element sourceElement) {

   // 註冊 AnnotationAwareAspectJAutoProxyCreator
   BeanDefinition beanDefinition = AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(
         parserContext.getRegistry(), parserContext.extractSource(sourceElement));
   // 處理 proxy-target-class 和 expose-proxy屬性
   useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement);
   // 註冊 BeanComponentDefinition
   registerComponentIfNecessary(beanDefinition, parserContext);
}
複製代碼

這裏分爲三個步驟

1:經過調用AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary方法進行註冊AnnotationAwareAspectJAutoProxyCreator類。

2:處理aop:aspectj-autoproxy標籤的 proxy-target-classexpose-proxy 屬性。

3:註冊 BeanComponentDefinition ,就是對 AnnotationAwareAspectJAutoProxyCreator 的封裝。具體做用後續補上

先來看第一步註冊的源碼

@Nullable
private static BeanDefinition registerOrEscalateApcAsRequired( Class<?> cls, BeanDefinitionRegistry registry, @Nullable Object source) {

   Assert.notNull(registry, "BeanDefinitionRegistry must not be null");

   if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
      BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
      if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
         int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
         int requiredPriority = findPriorityForClass(cls);
         if (currentPriority < requiredPriority) {
            apcDefinition.setBeanClassName(cls.getName());
         }
      }
      return null;
   }

   RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
   beanDefinition.setSource(source);
   beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
   // 定義角色,徹底內部使用
   beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
   registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
   return beanDefinition;
}
複製代碼

這裏就沒什麼好說的,先判斷一下是否已經註冊,若是已經註冊,則判斷優先級,若是已註冊優先級高則直接結束,反之直接建立RootBeanDefinition,經過調用DefaultListableBeanFactory的registerBeanDefinition方法進行bean註冊

優先級判斷代碼

private static final List<Class<?>> APC_PRIORITY_LIST = new ArrayList<>(3);

static {
   // Set up the escalation list...
   APC_PRIORITY_LIST.add(InfrastructureAdvisorAutoProxyCreator.class);
   APC_PRIORITY_LIST.add(AspectJAwareAdvisorAutoProxyCreator.class);
   APC_PRIORITY_LIST.add(AnnotationAwareAspectJAutoProxyCreator.class);
}
複製代碼
private static int findPriorityForClass(@Nullable String className) {
   for (int i = 0; i < APC_PRIORITY_LIST.size(); i++) {
      Class<?> clazz = APC_PRIORITY_LIST.get(i);
      if (clazz.getName().equals(className)) {
         return i;
      }
   }
   throw new IllegalArgumentException(
         "Class name [" + className + "] is not a known auto-proxy creator class");
}
複製代碼

註冊說完了繼續看一下對 proxy-target-classexpose-proxy 屬性的處理

private static void useClassProxyingIfNecessary(BeanDefinitionRegistry registry, @Nullable Element sourceElement) {
   if (sourceElement != null) {
       // 對標籤 proxy-target-class 的處理,使用方法 <aop:config proxy-target-class = "true"> 或 <aop:aspectj-autoproxy proxy-target-class="true"/> 使用
      // 其做用是 強制使用 CGLIB 代理,設置<aop:aspectj-autoproxy proxy-target-class="true"/> ,或須要使用CGLIB 和 @Aspectj自動代理支持 屬性 <aop:aspectj-autoproxy proxy-target-class="true"/>
      // JDK動態代理須要至少實現一個藉口 CGLIB 不須要實現接口
      boolean proxyTargetClass = Boolean.parseBoolean(sourceElement.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE));
      if (proxyTargetClass) {
         AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
      }

      // 對 expose-proxy 的處理 其做用是實現 目標對象內部方法調用可實現切面的加強
      // 例如 例如 A類中 c方法 調用 A類中的 d方法是沒法實時切面加強的,須要設置 <aop:aspectj-autoproxy expose-proxy="true"/> 例如 d 方法 有 @Transaction 註解則失效
         boolean exposeProxy = Boolean.parseBoolean(sourceElement.getAttribute(EXPOSE_PROXY_ATTRIBUTE));
      if (exposeProxy) {
         AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
      }
   }
}
複製代碼

proxy-target-class屬性的做用是強制使用 CGLIB 代理。

expose-proxy屬性的做用是目標對象內部方法調用可實現切面的加強,例如Test類中的A,B方法,A調用B方法進行數據庫save操做,B方法上有**@Transactional註解,若是A直接調用B方法則事物是不起做用的,須要設置expose-proxy=true**,而後使用 ((A)AopContext.currentProxy()).b() 調用方式。

註冊總體完成後,看一下主方法的extendBeanDefinition方法,擴展BeanDefinition。

private void extendBeanDefinition(Element element, ParserContext parserContext) {
   BeanDefinition beanDef =
      parserContext.getRegistry().getBeanDefinition(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME);
   if (element.hasChildNodes()) {
      addIncludePatterns(element, parserContext, beanDef);
   }
}

private void addIncludePatterns(Element element, ParserContext parserContext, BeanDefinition beanDef) {
   ManagedList<TypedStringValue> includePatterns = new ManagedList<>();
   NodeList childNodes = element.getChildNodes();
   for (int i = 0; i < childNodes.getLength(); i++) {
      Node node = childNodes.item(i);
      if (node instanceof Element) {
         Element includeElement = (Element) node;
         TypedStringValue valueHolder = new TypedStringValue(includeElement.getAttribute("name"));
         valueHolder.setSource(parserContext.extractSource(includeElement));
         includePatterns.add(valueHolder);
      }
   }
   if (!includePatterns.isEmpty()) {
      includePatterns.setSource(parserContext.extractSource(element));
      beanDef.getPropertyValues().add("includePatterns", includePatterns);
   }
}
複製代碼

extendBeanDefinition方法主要是對註冊的AnnotationAwareAspectJAutoProxyCreator就行擴充,若是aop:aspectj-autoproxy標籤還有子標籤,也就是**aop:include/標籤,則會做getPropertyValues.add的操做,這裏的aop:include/標籤若是存在對於解析@Aspect標註的類時有一個match的動做,這裏的內容會在buildAspectJAdvisors**方法中詳細講解。 好了到這裏整個Xml的解析註冊就完成了,接下來看一下具體的實現。

4 SpringBoot 自動配置Aop

5 Aop實現源碼分析

5.1 術語定義

  1. ClassFilter:類過濾器
  2. Advisor:通知器
  3. targetClass:目標類,或稱被代理的原始類
  4. Advice:通知,或稱攔截器,也就是要加強的代碼邏輯
  5. MethodMatcher:方法匹配器
  6. Pointcut:切點,由ClassFilterMethodMatcher組成

ClassFilter用於約束一個Advisor(通知器),與指定的targetClass是否匹配,只有匹配的前提下,Advisor才能使用其內部持有的Advice(加強器)targetClass進行加強。

Advisor分兩大類:IntroductionAdvisor(引介通知器)和PointcutAdvisor(切點通知器)。兩類Advisor都是爲了加強targetClass,可是做用不同。IntroductionAdvisor主要爲了給targetClass追加接口(或者說追加更多的方法),這種加強屬於類級別的加強;而PointcutAdvisor主要爲了攔截方法,這種加強屬於方法級別的加強。

​ 正是因爲兩類Advisor的加強級別不一樣,而致使了對ClassFilter的使用方式不一樣。IntroductionAdvisor進行類級別加強,所以只須要直接持有ClassFilter便可;而PointcutAdvisor進行方法級別加強,所以須要同時使用ClassFilterMethodMatcher(方法匹配器)。PointcutAdvisor內部持有一個Pointcut,而Pointcut就是由ClassFilter和MethodMatcher組成的

5.2 AnnotationAwareAspectJAutoProxyCreator類解析

​ 在上面的xml解析aop:aspectj-autoproxy標籤時,一直都在說註冊AnnotationAwareAspectJAutoProxyCreator,其實它是繼承了InstantiationAwareBeanPostProcessor -> BeanPostProcessor的,繼承了 InstantiationAwareBeanPostProcessor 會在實例化以前執行postProcessBeforeInstantiationpostProcessAfterInstantiation方法,但同時它也間接性繼承了BeanPostProcessor,也會在初始化先後執行 postProcessBeforeInstantiationpostProcessAfterInitialization 方法,在createBean方法中有這麼一方法resolveBeforeInstantiation,它就是在實例化以前執行的InstantiationAwareBeanPostProcessor,代碼以下:

@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException {

************
   try {
      // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
      // 給BeanPostProcessors一個返回代理而不是目標bean實例的機會
      Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
      if (bean != null) {
         return bean;
      }
   }
   catch (Throwable ex) {
      throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
            "BeanPostProcessor before instantiation of bean failed", ex);
   }
**********
}
複製代碼

5.2.1 postProcessBeforeInstantiation方法解析

​ 建立代理的真正方法就是AbstractAutoProxyCreator.postProcessAfterInitialization方法或 postProcessBeforeInstantiation方法。AbstractAutoProxyCreatorAnnotationAwareAspectJAutoProxyCreator的超類。下面先看一下 AbstractAutoProxyCreator.postProcessBeforeInstantiation方法

@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
		Object cacheKey = getCacheKey(beanClass, beanName);

    // 判斷beanName是否爲空 和 targetSoucedBeans是否包含beanName
		if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
			if (this.advisedBeans.containsKey(cacheKey)) {
				return null;
			}
			// isInfrastructureClass:是不是基礎架構類 就是是不是 aop配置類, 若是是 則不可代理這種
			// shouldSkip 這裏經過調用子類 AspectJAwareAdvisorAutoProxyCreator 的 shouldSkip 方法,經過獲取所有的Advisor,來判斷Advisor所屬的bean和入參bean是不是同一個,若是是則不用加強反之能夠,
			// 而後會調用 super.shouldSkip 排除 bean 名稱 尾部是 .ORIGINA 結尾的bean
			if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
				this.advisedBeans.put(cacheKey, Boolean.FALSE);
				return null;
			}
		}

		// Create proxy here if we have a custom TargetSource.
		// Suppresses unnecessary default instantiation of the target bean:
		// The TargetSource will handle target instances in a custom fashion.

		// 若是有自定義的targetSouce 在這裏就直接建立代理,不須要等到實例化的時候在建立,避免沒必要要的bean建立實例化
		TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
		if (targetSource != null) {
			if (StringUtils.hasLength(beanName)) {
				// 緩存
				this.targetSourcedBeans.add(beanName);
			}
			// 獲取該bean可用的加強器 就是循環掃描配置類 , 掃出全部的 before alter around 等
			Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
			
			// 根據加強器建立代理
			Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
			// 緩存
			this.proxyTypes.put(cacheKey, proxy.getClass());
			return proxy;
		}

		return null;
	}
複製代碼

postProcessBeforeInstantiation方法主要作了一下幾件事情:

1: 判斷beanName是否爲空 和 targetSoucedBeans是否包含beanName

2: 判斷是不是基礎架構類,就是是不是aop配置類, 若是是則不可代理這種,直接返回,還會在判斷 shouldSkip ,這裏經過調用子類 AspectJAwareAdvisorAutoProxyCreatorshouldSkip 方法,經過獲取所有的Advisor,來判斷Advisor所屬的bean和入參bean是不是同一個,若是是**(若是是則就是表明該bean是配置類)則不能夠加強反之能夠,而後會調用 super.shouldSkip 排除尾部是 .ORIGINA 結尾的bean**。

3: 獲取自定義的TargetSouce ,若是存在就直接建立代理,不須要等到實例化的時候在建立,避免沒必要要的bean建立實例化。

4: 若是存在自定義的TargetSouce,則獲取該bean可用的加強器 就是循環掃描配置類 , 掃出全部的 before alter around ,找到符合該bean的加強器。

5: 根據查詢出來的加強器建立代理並返回。

上述是對存在TargetSource狀況的描述,下面咱們看一下不存在的狀況

5.2.2 postProcessAfterInitialization方法解析

接下來看AbstractAutoProxyCreator類中的postProcessAfterInitialization方法

@Override
	public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
		if (bean != null) {
			Object cacheKey = getCacheKey(bean.getClass(), beanName);
      //防止 bean 屢次被加強。
			if (this.earlyProxyReferences.remove(cacheKey) != bean) {
        // 若是須要 則建立代理
				return wrapIfNecessary(bean, beanName, cacheKey);
			}
		}
		return bean;
	}
複製代碼
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
   if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
      return bean;
   }
  // 判斷是不是 基礎配置類或須要跳過的類。若是是則不加強 在下邊的方法中會put
   if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
      return bean;
   }
  
   // isInfrastructureClass:是不是基礎架構類 就是是不是 aop配置類, 若是是 則不可代理這種
		// shouldSkip 這裏經過調用子類 AspectJAwareAdvisorAutoProxyCreator 的 shouldSkip 方法,經過獲取所有的Advisor,來判斷Advisor 所屬的 bean 和入參 bean 是不是同一個,若是是則不用加強反之能夠,
		// 而後會調用 super.shouldSkip 排除 bean 名稱 尾部是 .ORIGINA 結尾的bean
   if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
      this.advisedBeans.put(cacheKey, Boolean.FALSE);
      return bean;
   }

   // Create proxy if we have advice.
  // 獲取該bean的加強器,若是有
   Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
  // 若是加強器不是空 則建立代理
   if (specificInterceptors != DO_NOT_PROXY) {
      this.advisedBeans.put(cacheKey, Boolean.TRUE);
     // 建立代理並返回
      Object proxy = createProxy(
            bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
     // 增長緩存
      this.proxyTypes.put(cacheKey, proxy.getClass());
      return proxy;
   }

   // 增長緩存 若是不符合則話put,在上邊的判斷直接返回
   this.advisedBeans.put(cacheKey, Boolean.FALSE);
   return bean;
}
複製代碼

來解析wrapIfNecessary方法都作了什麼

1: 判斷是不是 基礎配置類或須要跳過的類。

2: 判斷是不是基礎架構類,就是是不是aop配置類, 若是是則不可代理這種,直接返回,還會在判斷 shouldSkip ,這裏經過調用子類 AspectJAwareAdvisorAutoProxyCreatorshouldSkip 方法,經過獲取所有的Advisor,來判斷Advisor所屬的bean和入參bean是不是同一個,若是是**(若是是則就是表明該bean是配置類)則不能夠加強反之能夠,而後會調用 super.shouldSkip 排除尾部是 .ORIGINA 結尾的bean**。

3: 獲取該bean符合的加強器。

4: 建立代理並返回。

5: 增長緩存。

6: 增長緩存 若是存在可用的加強器,則將該bean設置爲false,在1中會進行判斷。

獲取加強器
5.2.2.1 findCandidateAdvisors方法獲取所有加強器

wrapIfNecessary中最重要的方法就是getAdvicesAndAdvisorsForBean方法,經過getAdvicesAndAdvisorsForBean方法能夠獲取到適合bean加強器,接下來就看看它吧。

@Override
@Nullable
protected Object[] getAdvicesAndAdvisorsForBean(
      Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) {

   // 獲取該bean可使用的加強器
   List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName);
   if (advisors.isEmpty()) {
      return DO_NOT_PROXY;
   }
   return advisors.toArray();
}
複製代碼
protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
   // 獲取全部加強器
   List<Advisor> candidateAdvisors = findCandidateAdvisors();
   // 獲取當前bean可使用的加強器
   List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
   extendAdvisors(eligibleAdvisors);
   if (!eligibleAdvisors.isEmpty()) {
      eligibleAdvisors = sortAdvisors(eligibleAdvisors);
   }
   return eligibleAdvisors;
}
複製代碼

findEligibleAdvisors方法中首先獲取了全部的加強器,而後獲取適合bean的加強器,先看一下findCandidateAdvisors方法。

這裏首先執行子類findCandidateAdvisors方法,也就是AnnotationAwareAspectJAutoProxyCreator的。

@Override
	protected List<Advisor> findCandidateAdvisors() {
		// Add all the Spring advisors found according to superclass rules.
		// 調用父類 findCandidateAdvisors 方法
		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方法,也就是處理xml形式的Aop配置,而後執行了buildAspectJAdvisors方法,處理註解形式的aop配置

先看一下父類的findCandidateAdvisors方法

protected List<Advisor> findCandidateAdvisors() {
   Assert.state(this.advisorRetrievalHelper != null, "No BeanFactoryAdvisorRetrievalHelper available");
   return this.advisorRetrievalHelper.findAdvisorBeans();
}
複製代碼
public List<Advisor> findAdvisorBeans() {
   // Determine list of advisor bean names, if not cached already.
   // 獲取緩存的 aop配置類名字,也就是 advisorBeanNames 數組中的信息
   String[] advisorNames = this.cachedAdvisorBeanNames;
   if (advisorNames == null) {
      // Do not initialize FactoryBeans here: We need to leave all regular beans
      // uninitialized to let the auto-proxy creator apply to them!
      // 若是 cachedAdvisorBeanNames 不存在則經過BeanFactoryUtils 獲取,條件是 根據類型獲取
      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) {
      // 是不是有資格的bean
      if (isEligibleBean(name)) {
         // bean是否正在建立
         if (this.beanFactory.isCurrentlyInCreation(name)) {
            if (logger.isTraceEnabled()) {
               logger.trace("Skipping currently created advisor '" + name + "'");
            }
         }
         else {
            try {
               // 存入到advisors中
               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());
                     }
                     // Ignore: indicates a reference back to the bean we're trying to advise.
                     // We want to find advisors other than the currently created bean itself.
                     continue;
                  }
               }
               throw ex;
            }
         }
      }
   }
   return advisors;
}
複製代碼

父類方法中主要作了兩件事:

1: 首先先從緩存cachedAdvisorBeanNames中獲取,看是否存在Aop的配置類,若是不存在則經過BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, Advisor.class, true, false) 方法獲取Aop的配置類,賦值到緩存中。

2:循環解析BeanName,經過beanFactory.isCurrentlyInCreation方法判斷beanName是不是正在建立狀態,若是不是則add到advisors中。

接下來看一下buildAspectJAdvisors方法,處理註解形式aop的配置類

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<>();
            // 獲取全部的bean
            String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
                  this.beanFactory, Object.class, true, false);
            // 循環全部的bean 來獲取加強器
            for (String beanName : beanNames) {
               // 判斷bean是否有資格 默認全部bean都是有資格的 也能夠配置 正則表達式 來判斷哪些@aspet bean 是資格的
               // 檢查給定的bean是否有資格進行自動代理。
               // 若是沒有使用<aop:include>元素,則將 includePatterns null 並指定全部bean都是有資格的。若是「includePatterns」爲非null,則須要和include中的name機型匹配。
               // 例如 @Aspect標註的類是 config <aop:include name="config1"/> include的name是 config1 則不匹配, 則@Aspect標註的不生效
               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.
               // 獲取bean的type
               Class<?> beanType = this.beanFactory.getType(beanName);
               if (beanType == null) {
                  continue;
               }
               // 判斷是不是 aop配置bean 也就是是否被@Aspect註解標註
               if (this.advisorFactory.isAspect(beanType)) {
                  aspectNames.add(beanName);
                  // 構建成AspectMetadata類
                  AspectMetadata amd = new AspectMetadata(beanType, beanName);

                  // 判斷@Aspect註解中標註的是否爲singleton類型,默認的切面類都是singleton類型
                  if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) {
                     MetadataAwareAspectInstanceFactory factory =
                           new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName);
                     // 獲取加強器
                     List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory);
                     // put cache
                     if (this.beanFactory.isSingleton(beanName)) {
                        this.advisorsCache.put(beanName, classAdvisors);
                     } else {
                        this.aspectFactoryCache.put(beanName, factory);
                     }
                     // add advisors
                     advisors.addAll(classAdvisors);
                  } else {
                     // Per target or per this.
                     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;
}
複製代碼

buildAspectJAdvisors方法中,主要作了如下事情:

1: 首先獲取全部的bean,經過BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, Object.class, true, false)。

2: 判斷bean是否有資格,默認全部bean都是有資格的,也能夠經過配置includePatterns來判斷哪些@aspet bean 是資格的。

配置includePatterns:

使用aop:include/標籤,若是配置aop:include/元素,includePatterns 爲非null,則須要和include中的name機型匹配。

若是不配置,則 includePatterns 爲 null 並指定全部bean都是有資格的。

例如 @Aspect標註的類是 config <aop:include name="config1"/> include的name是 config1 則不匹配, 則@Aspect標註的不生效。

3: 判斷bean是不是Aop配置類,也就是是否被@Aspect標識。

4: 經過this.advisorFactory.getAdvisors(factory)方法獲取bean的加強器

5: 返回所有加強器, 其中2 3 4 5都是for循環中操做。

buildAspectJAdvisors方法中,最重要的就是步驟4,獲取每個bean的加強器,接着看this.advisorFactory.getAdvisors(factory) 方法:

@Override
public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory aspectInstanceFactory) {
   // 獲取 @Aspect 標註Bean 類型
   Class<?> aspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();
   // 獲取 @Aspect 標註Bean 名字
   String aspectName = aspectInstanceFactory.getAspectMetadata().getAspectName();
   // 進行bean驗證
   validate(aspectClass);

   // We need to wrap the MetadataAwareAspectInstanceFactory with a decorator
   // so that it will only instantiate once.
   MetadataAwareAspectInstanceFactory lazySingletonAspectInstanceFactory =
         new LazySingletonAspectInstanceFactoryDecorator(aspectInstanceFactory);

   List<Advisor> advisors = new ArrayList<>();
   // 獲取除了 標記有 Pointcut 註解 的全部方法
   for (Method method : getAdvisorMethods(aspectClass)) {
      // 獲取每一個方法上的 加強器
      Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, advisors.size(), aspectName);
      if (advisor != null) {
         // add
         advisors.add(advisor);
      }
   }

   // If it's a per target aspect, emit the dummy instantiating aspect.
   if (!advisors.isEmpty() && lazySingletonAspectInstanceFactory.getAspectMetadata().isLazilyInstantiated()) {
      Advisor instantiationAdvisor = new SyntheticInstantiationAdvisor(lazySingletonAspectInstanceFactory);
      advisors.add(0, instantiationAdvisor);
   }

   // Find introduction fields.
   for (Field field : aspectClass.getDeclaredFields()) {
      Advisor advisor = getDeclareParentsAdvisor(field);
      if (advisor != null) {
         advisors.add(advisor);
      }
   }

   return advisors;
}
複製代碼

getAdvisors方法中,經過getAdvisorMethods方法獲取到了除了標記有 Pointcut 註解的其餘全部方法,而後經過getAdvisor方法獲取每一個方法上的加強器。

getAdvisorMethods方法源碼:

private List<Method> getAdvisorMethods(Class<?> aspectClass) {
   final List<Method> methods = new ArrayList<>();
   // 獲取除了 標記有 Pointcut 註解 的全部方法
   ReflectionUtils.doWithMethods(aspectClass, method -> {
      // Exclude pointcuts
      if (AnnotationUtils.getAnnotation(method, Pointcut.class) == null) {
         methods.add(method);
      }
   });
   methods.sort(METHOD_COMPARATOR);
   return methods;
}
複製代碼

getAdvisor方法源碼:

@Override
@Nullable
public Advisor getAdvisor(Method candidateAdviceMethod, MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrderInAspect, String aspectName) {

   // 驗證
   validate(aspectInstanceFactory.getAspectMetadata().getAspectClass());

   // 獲取切點
   AspectJExpressionPointcut expressionPointcut = getPointcut(
         candidateAdviceMethod, aspectInstanceFactory.getAspectMetadata().getAspectClass());
   if (expressionPointcut == null) {
      return null;
   }

   // 實例化 加強器 這裏使用的是PointcutAdvisor通知器,是方法級別的
   return new InstantiationModelAwarePointcutAdvisorImpl(expressionPointcut, candidateAdviceMethod,
         this, aspectInstanceFactory, declarationOrderInAspect, aspectName);
}
複製代碼

getAdvisor方法中首先作了bean類型的驗證,而後獲取切點,最後開始實例化加強器, 這裏實例化加強器使用的是PointcutAdvisor(通知器),實際上InstantiationModelAwarePointcutAdvisorImpl是PointcutAdvisor的一個實現,也就是方法級別的通知器

先看beanType的驗證,validate方法源碼:

@Override
public void validate(Class<?> aspectClass) throws AopConfigException {
   // If the parent has the annotation and isn't abstract it's an error
  // 是否存在@Aspect註解 和 abstract判斷
   if (aspectClass.getSuperclass().getAnnotation(Aspect.class) != null &&
         !Modifier.isAbstract(aspectClass.getSuperclass().getModifiers())) {
      throw new AopConfigException("[" + aspectClass.getName() + "] cannot extend concrete aspect [" +
            aspectClass.getSuperclass().getName() + "]");
   }

   AjType<?> ajType = AjTypeSystem.getAjType(aspectClass);
   // 判斷bean類型是不是 Aspect
   if (!ajType.isAspect()) {
      throw new NotAnAtAspectException(aspectClass);
   }
   // 判斷 bean 的 kind 是不是 PERCFLOW PERCFLOWBELOW 這兩種在AOP中是不支持的
   if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOW) {
      throw new AopConfigException(aspectClass.getName() + " uses percflow instantiation model: " +
            "This is not supported in Spring AOP.");
   }
   if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOWBELOW) {
      throw new AopConfigException(aspectClass.getName() + " uses percflowbelow instantiation model: " +
            "This is not supported in Spring AOP.");
   }
}
複製代碼

接下來看獲取加強器切點信息getPointcut方法源碼:

@Nullable
private AspectJExpressionPointcut getPointcut(Method candidateAdviceMethod, Class<?> candidateAspectClass) {
   // 獲取 判斷加強器類型
   AspectJAnnotation<?> aspectJAnnotation =
         AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
   if (aspectJAnnotation == null) {
      return null;
   }

   // 構建 AspectJExpressionPointcut 類
   AspectJExpressionPointcut ajexp =
         new AspectJExpressionPointcut(candidateAspectClass, new String[0], new Class<?>[0]);
   ajexp.setExpression(aspectJAnnotation.getPointcutExpression());
   if (this.beanFactory != null) {
      ajexp.setBeanFactory(this.beanFactory);
   }
   return ajexp;
}
複製代碼

經過findAspectJAnnotationOnMethod方法獲取到方法的加強器類型,而後構建AspectJExpressionPointcut類,

最後看一下實例化加強器的代碼:

public InstantiationModelAwarePointcutAdvisorImpl(AspectJExpressionPointcut declaredPointcut, Method aspectJAdviceMethod, AspectJAdvisorFactory aspectJAdvisorFactory, MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) {

   this.declaredPointcut = declaredPointcut;
   this.declaringClass = aspectJAdviceMethod.getDeclaringClass();
   this.methodName = aspectJAdviceMethod.getName();
   this.parameterTypes = aspectJAdviceMethod.getParameterTypes();
   this.aspectJAdviceMethod = aspectJAdviceMethod;
   this.aspectJAdvisorFactory = aspectJAdvisorFactory;
   this.aspectInstanceFactory = aspectInstanceFactory;
   this.declarationOrder = declarationOrder;
   this.aspectName = aspectName;

   if (aspectInstanceFactory.getAspectMetadata().isLazilyInstantiated()) {
      // Static part of the pointcut is a lazy type.
      Pointcut preInstantiationPointcut = Pointcuts.union(
            aspectInstanceFactory.getAspectMetadata().getPerClausePointcut(), this.declaredPointcut);

      // Make it dynamic: must mutate from pre-instantiation to post-instantiation state.
      // If it's not a dynamic pointcut, it may be optimized out
      // by the Spring AOP infrastructure after the first evaluation.
      this.pointcut = new PerTargetInstantiationModelPointcut(
            this.declaredPointcut, preInstantiationPointcut, aspectInstanceFactory);
      this.lazy = true;
   }
   else {
      // A singleton aspect.
      this.pointcut = this.declaredPointcut;
      this.lazy = false;
      // 實例化加強器
      this.instantiatedAdvice = instantiateAdvice(this.declaredPointcut);
   }
}
複製代碼

在這裏主要關注instantiateAdvice方法,實際上它的做用就是實例化Advice

@Override
@Nullable
public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut expressionPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) {

   // 獲取 @Aspect 標註de bean的類型
   Class<?> candidateAspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();

   // 驗證bean
   validate(candidateAspectClass);

   // 獲取方法上的加強器
   AspectJAnnotation<?> aspectJAnnotation =
         AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
   if (aspectJAnnotation == null) {
      return null;
   }

   // If we get here, we know we have an AspectJ method.
   // Check that it's an AspectJ-annotated class
   // 檢查 bean 是不是被 @Aspect標註
   if (!isAspect(candidateAspectClass)) {
      throw new AopConfigException("Advice must be declared inside an aspect type: " +
            "Offending method '" + candidateAdviceMethod + "' in class [" +
            candidateAspectClass.getName() + "]");
   }

   if (logger.isDebugEnabled()) {
      logger.debug("Found AspectJ method: " + candidateAdviceMethod);
   }

   AbstractAspectJAdvice springAdvice;

   // 實例化 加強器 根據不一樣的類型實例化不經過的加強器
   switch (aspectJAnnotation.getAnnotationType()) {
      case AtPointcut:
         if (logger.isDebugEnabled()) {
            logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'");
         }
         return null;
      case AtAround:
         springAdvice = new AspectJAroundAdvice(
               candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
         break;
      case AtBefore:
         springAdvice = new AspectJMethodBeforeAdvice(
               candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
         break;
      case AtAfter:
         springAdvice = new AspectJAfterAdvice(
               candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
         break;
      case AtAfterReturning:
         springAdvice = new AspectJAfterReturningAdvice(
               candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
         AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation();
         if (StringUtils.hasText(afterReturningAnnotation.returning())) {
            springAdvice.setReturningName(afterReturningAnnotation.returning());
         }
         break;
      case AtAfterThrowing:
         springAdvice = new AspectJAfterThrowingAdvice(
               candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
         AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation();
         if (StringUtils.hasText(afterThrowingAnnotation.throwing())) {
            springAdvice.setThrowingName(afterThrowingAnnotation.throwing());
         }
         break;
      default:
         throw new UnsupportedOperationException(
               "Unsupported advice type on method: " + candidateAdviceMethod);
   }

   // Now to configure the advice...
   springAdvice.setAspectName(aspectName);
   springAdvice.setDeclarationOrder(declarationOrder);
   String[] argNames = this.parameterNameDiscoverer.getParameterNames(candidateAdviceMethod);
   if (argNames != null) {
      springAdvice.setArgumentNamesFromStringArray(argNames);
   }
   springAdvice.calculateArgumentBindings();

   return springAdvice;
}
複製代碼

首先這裏仍是會首先驗證bean的類型,經過validate方法,接下來獲取method的加強器,依然是經過findAspectJAnnotationOnMethod方法,緊接着判斷bean是否被@Aspect標註,最後經過switch語法實例化加強器 並賦值一些參數配置。

到這裏咱們已經獲取到了上下文中全部的可用加強器,到此findCandidateAdvisors方法的代碼就所有都解析完成了,若是尚未明白總體流程,能夠看一下下方時序圖

5.2.2.2 findAdvisorsThatCanApply匹配適用bean的加強器

接下來根據解析到的加強器進行匹配,查找出適用於須要實例化bean的加強器findAdvisorsThatCanApply方法源碼:

protected List<Advisor> findAdvisorsThatCanApply( List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName) {

   // 設置當前代理對象名稱
   ProxyCreationContext.setCurrentProxiedBeanName(beanName);
   try {
      // 匹配
      return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);
   }
   finally {
      // 刪除當前代理對象名稱
      ProxyCreationContext.setCurrentProxiedBeanName(null);
   }
}
複製代碼
public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz) {
		if (candidateAdvisors.isEmpty()) {
			return candidateAdvisors;
		}
		List<Advisor> eligibleAdvisors = new ArrayList<>();
		for (Advisor candidate : candidateAdvisors) {
			// 在這裏對 IntroductionAdvisor 類型的 Advisor(通知器)作會處理, 由於IntroductionAdvisor 是處理類攔截級別的
			// 僅須要使用classFilter 便可
			if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {
				eligibleAdvisors.add(candidate);
			}
		}
		boolean hasIntroductions = !eligibleAdvisors.isEmpty();
		for (Advisor candidate : candidateAdvisors) {
			// 對 處理過Advisor 的作 跳過處理
			if (candidate instanceof IntroductionAdvisor) {
				// already processed
				continue;
			}
			// 在這裏對 PointcutAdvisor 類型的 Advisor(通知器) 進行處理,由於 PointcutAdvisor 是方法級別的攔截,須要作 ClassFilter 和 MethodMatcher 判斷
			if (canApply(candidate, clazz, hasIntroductions)) {
				// 將匹配的加強器 add 
				eligibleAdvisors.add(candidate);
			}
		}
		// 返回bean匹配的加強器
		return eligibleAdvisors;
	}
複製代碼

在上述代碼中,能夠看到,這裏會對Advisor(通知器)作判斷處理,分別是IntroductionAdvisorPointcutAdvisor,他們都分別繼承了Advisor, 在本文中是使用PointcutAdvisor的實現類InstantiationModelAwarePointcutAdvisorImpl

區別

IntroductionAdvisor: IntroductionAdvisor主要爲了給targetClass追加接口(或者說追加更多的方法),這種加強屬於類級別的加強,因此只須要作ClassFilter判斷。

PointcutAdvisor: PointcutAdvisor主要爲了攔截方法,這種加強屬於方法級別的加強,則須要作ClassFilterMethodMatcher的判斷。

在findAdvisorsThatCanApply方法中最重要的就是canApply方法,直接看一下源碼:

public static boolean canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions) {
   Assert.notNull(pc, "Pointcut must not be null");
   // 首先ClassFilter 進行匹配
   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;
}
複製代碼

在canApply方法中首先作了ClassFilter的匹配判斷,接下來獲取到Class的所有public方法,遍歷全部方法,進行MethodMatcher的匹配操做,最終將匹配到的Advisor所有返回,到這裏findAdvisorsThatCanApply方法就所有解析完成了。

6 建立代理

5.2 中已經將適配於bean的加強器(Advice)獲取到了, 接下來繼續分析主流程wrapIfNecessary方法中的createProxy方法。

protected Object createProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource) {

		// 曝光 目標類
		if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
			AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
		}

		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.copyFrom(this);

		// 判斷 是否設置了 ProxyTargetClass 是否使用CGLIB
		if (!proxyFactory.isProxyTargetClass()) {
			// 若是沒有設置 ProxyTargetClass 則須要判斷 beanClass 是否應該使用CGLIB 反之使用 JDK動態代理
			if (shouldProxyTargetClass(beanClass, beanName)) {
				proxyFactory.setProxyTargetClass(true);
			} else {
				evaluateProxyInterfaces(beanClass, proxyFactory);
			}
		}

		// 設置bean的加強器和攔截器
		Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
		proxyFactory.addAdvisors(advisors);
		// 設置目標類
		proxyFactory.setTargetSource(targetSource);
		customizeProxyFactory(proxyFactory);

		proxyFactory.setFrozen(this.freezeProxy);
		if (advisorsPreFiltered()) {
			proxyFactory.setPreFiltered(true);
		}

		return proxyFactory.getProxy(getProxyClassLoader());
	}
複製代碼

上面的方法其實就是封裝了ProxyFactory,真是建立代理的仍是ProxyFactory,接下來看一下getProxy接口。

public Object getProxy(@Nullable ClassLoader classLoader) {
   return createAopProxy().getProxy(classLoader);
}
複製代碼
@Override
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
   // 判斷使用 CGLIB 仍是 JDK動態代理

   // isOptimize : 是否優化處理
   // isProxyTargetClass 是否使用CGLIB代理
   // hasNoUserSuppliedProxyInterfaces 是否實現了接口
   if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
      Class<?> targetClass = config.getTargetClass();
      if (targetClass == null) {
         throw new AopConfigException("TargetSource cannot determine target class: " +
               "Either an interface or a target is required for proxy creation.");
      }
      // 若是是實現了接口 則 使用JDK代理
      if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
         return new JdkDynamicAopProxy(config);
      }
      // 使用CGLIB代理
      return new ObjenesisCglibAopProxy(config);
   }
   else {
      // 使用JDK代理
      return new JdkDynamicAopProxy(config);
   }
}
複製代碼

接下來就是調用getProxy方法進行CGLIB或者JDK代理建立了,而後返回代理類。

CBLIB 和 JDK 代理源碼後續文章講解。

GItHub : github.com/lantaoGitHu…

參考:my.oschina.net/lixin91/blo…

相關文章
相關標籤/搜索