Springboot源碼分析之代理三板斧

  • 2019 年 10 月 3 日
  • 筆記

摘要:

Spring的版本變遷過程中,註解發生了很多的變化,然而代理的設計也發生了微妙的變化,從Spring1.xProxyFactoryBean的硬編碼到Spring2.xAspectj註解,最後到了現在廣為熟知的自動代理。

file

說明:

  • ProxyConfig代理的相關配置類
  • AdvisedSupport實現了Advised,封裝了對AdviceAdvisor的操作
  • ProxyCreatorSupport該類及其子類主要是利用代理工廠幫助創建jdk或者cglib的代理對象
  • ProxyProcessorSupport該類及其子類才是我們目前用得做多的,利用後置處理器來進行自動代理處理

ProxyFactoryBean

    package com.github.dqqzj.springboot.aop;        import org.springframework.aop.MethodBeforeAdvice;      import org.springframework.aop.TargetSource;      import org.springframework.aop.framework.ProxyFactoryBean;      import org.springframework.aop.target.SingletonTargetSource;      import org.springframework.context.annotation.Bean;      import org.springframework.stereotype.Component;        import java.lang.reflect.Method;        /**       * @author qinzhongjian       * @date created in 2019-08-24 11:05       * @description: TODO       * @since JDK 1.8.0_212-b10       */      @Component      public class MyMethodBeforeAdvice implements MethodBeforeAdvice {          @Override          public void before(Method method, Object[] args, Object target) throws Throwable {              if (!method.getName().equals("toString")) {                  System.out.println(target.getClass().getName() + "#" + method.getName());              }          }          /**           * 代理的目標對象  效果同setTargetSource(@Nullable TargetSource targetSource)           * TargetSource targetSource = new SingletonTargetSource(aopService);           * 可以從容器獲取,也可以類似下面這樣直接new,使用區別需要熟悉spring機制。           * factoryBean.setTarget(new AopService());           *           * 設置需要被代理的介面  效果同factoryBean.setProxyInterfaces(new Class[]{AopService.class});           * 若沒有實現介面,那就會採用cglib去代理           * 如果有介面不指定的話會代理所有的介面,否則代理指定的介面           *           *  setInterceptorNames方法源程式碼中有這樣的一句話:Set the list of Advice/Advisor bean names. This must always be set           *  to use this factory bean in a bean factory.           */          @Bean          public ProxyFactoryBean proxyFactoryBean(AopService aopService) {              ProxyFactoryBean factoryBean = new ProxyFactoryBean();              factoryBean.setTarget(aopService);              //factoryBean.setInterfaces(AopService.class);                factoryBean.setInterceptorNames("myMethodBeforeAdvice");              //是否強制使用cglib,默認是false的              //factoryBean.setProxyTargetClass(true);              return factoryBean;          }        }

file

源碼分析:

        @Override          @Nullable          public Object getObject() throws BeansException {              //根據我們配置的interceptorNames來獲取對應的Advisor並加入通知器執行鏈中              initializeAdvisorChain();              if (isSingleton()) {                  //生成singleton的代理對象,會利用DefaultAopProxyFactory去生成代理            //在內部如果你手動沒有去設置需要被代理的介面,Spring會代理你所有的實現介面。                  return getSingletonInstance();              }              else {                  if (this.targetName == null) {                      logger.warn("Using non-singleton proxies with singleton targets is often undesirable. " +                              "Enable prototype proxies by setting the 'targetName' property.");                  }            //和單利非常類似 只不過沒有快取了                  return newPrototypeInstance();              }          }          private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException {              if (this.advisorChainInitialized) {                  return;              }              if (!ObjectUtils.isEmpty(this.interceptorNames)) {                  // 最後一個不能是全局的suffix *,除非我們指定了targetSource之類的                  if (this.interceptorNames[this.interceptorNames.length - 1].endsWith(GLOBAL_SUFFIX) &&                          this.targetName == null && this.targetSource == EMPTY_TARGET_SOURCE) {                      throw new AopConfigException("Target required after globals");                  }                  for (String name : this.interceptorNames) {                      // 如國攔截器的名稱是以*結尾的,說明它要去全局裡面都搜索出來                      // 全局:去自己容器以及父容器中找,類型為Advisor.class的,名稱是以這個名稱為開頭的prefix的Bean.                      if (name.endsWith(GLOBAL_SUFFIX)) {                          addGlobalAdvisor((ListableBeanFactory) this.beanFactory,                                  name.substring(0, name.length() - GLOBAL_SUFFIX.length()));                      }                      // 一般的情況下我們都是精確匹配                      else {                          Object advice;                          if (this.singleton || this.beanFactory.isSingleton(name)) {                              // 從容器里獲取該bean                              advice = this.beanFactory.getBean(name);                          }                          // 原型處理                          else {                              advice = new PrototypePlaceholderAdvisor(name);                          }                          addAdvisorOnChainCreation(advice, name);                      }                  }              }              this.advisorChainInitialized = true;          }        // 將advice對象添加到通知器鏈中          private void addAdvisorOnChainCreation(Object next, String name) {              // 這裡調用namedBeanToAdvisor做了一下適配:成統一的Advisor              Advisor advisor = namedBeanToAdvisor(next);              addAdvisor(advisor);          }      //方法中首先會調用namedBeanToAdvisor(next)方法,將從ioc容器獲取的普通對象轉換成通知器Advisor對象          private Advisor namedBeanToAdvisor(Object next) {              try {                  return this.advisorAdapterRegistry.wrap(next);              }          }

DefaultAdvisorAdapterRegistry

file

這個類還允許我們自定義適配器,然後註冊到裡面就行。

      @Override          public void registerAdvisorAdapter(AdvisorAdapter adapter) {              this.adapters.add(adapter);          }

ProxyFactoryBean脫離IoC容器使用

file

ProxyFactory

file

說明:這個類一般是spring自己內部使用的,我們自定義的話很難與容器進行整合,它一般都是返回的原型模式代理

AspectJProxyFactory

file

小結:

根據以上案例可以發現 都是首先進行AdvisedSupport的準備,然後交給子類ProxyCreatorSupport根據條件  得到JDK或者CGLIB的AopProxy,當代理對象被調用的時候在invoke或者intercept方法中會調用ProxyCreatorSupport的getInterceptorsAndDynamicInterceptionAdvice方法去初始化advice和各個方法之間的映射關係並快取
同類方法代理不生效原因?

很多時候會發現代理方法和非代理方法在同一個類中調用不生效和調用順序有關係,我們進行重構程式碼來分析一下原因

    public class AspectJProxyFactoryApplication {          public static void main(String[] args) {              AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new AopService());              // 注意:此處得MyAspect類上面的@Aspect註解必不可少              proxyFactory.addAspect(MyAspect.class);              //proxyFactory.setProxyTargetClass(true);//是否需要使用CGLIB代理              AopService proxy = proxyFactory.getProxy();              proxy.test();          }      }
    @Aspect      public class MyAspect {          //@Pointcut("execution(* com.github..aop.*.*(..))")          @Pointcut("execution(* com.github..aop.AopService.hello(..))")          private void pointcut() {          }            @Before("pointcut()")          public void before() {              System.out.println("-----------MyAspect#before-----------");          }      }
    @Service      public class AopService {          public String hello() {              System.out.println("hello, AopService");              return "hello, AopService";          }          public String test() {              System.out.println("test");              return hello();          }      }

答案就是不會生效,究竟是什麼引起的呢?其實就是我上面的小結的最後一個知識點。

file

這個時候chain沒有我們的通知器在裡面,
file

file

最終按照我們的程式執行,下面進行修改切點表達式,如果上面的例子看的諮詢的話下面就可以忽略了,主要就是是否增強就是第一個入口函數能否匹配上我們的切點表達式後續的根本不會關心你是否能匹配上。

    @Aspect      public class MyAspect {          @Pointcut("execution(* com.github..aop.*.*(..))")          //@Pointcut("execution(* com.github..aop.AopService.hello(..))")          private void pointcut() {          }            @Before("pointcut()")          public void before() {              System.out.println("-----------MyAspect#before-----------");          }      }

file

file

處理完後就會按照下面程式碼正常流程執行完

if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {     return invokeJoinpoint();  }

如果同一個類的方法調用都想讓通知器生效怎麼辦?這個就必須要讓通知添加到執行鏈中才行,根據上面所講的內容就可以達到這個目的,然後繼續用此代理對象來調用該內嵌方法。