Spring(11) - Introductions進行類擴充套件方法

碼猿手發表於2021-04-22

  Introductions(引用),在 Aspect 中稱為型別間的宣告,使切面能夠宣告被通知的物件(攔截的物件)實現給定的介面,並提供該介面的實現。

  簡單點說可以將一個類的實現方法複製到未實現的類中,動態的擴充套件類沒有的方法。

  通過 @DeclareParents 註解進行宣告,宣告在一個父型別的屬性上(比如介面),其中註解的屬性 value 指定對哪些路徑(包)下的類進行類方法擴充套件,defaultImpl 指定以哪個類為模板。

  如下案例:

  1)切面類:

@Aspect
@Component
public class LogAspects {
    @DeclareParents(value = "com.hrh.aop.*", defaultImpl = IndexDao.class)
    public static Dao dao;

    @Pointcut("execution(public * com.hrh.aop.*.*(..))")
    public void pointcut() {

    }

    @Before("pointcut()")
    public void LogStart() {
        System.out.println("LogStart...");
    }
}

  2)介面和實現類:

public interface Dao{
    void query();
}

@Repository
public class IndexDao implements Dao{
    public void query(){
        System.out.println("query:"+this.getClass().getSimpleName());
    }
}

  3)配置類:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.hrh.aop")
public class Config {
}

  4)空類:

@Repository("orderDao")
public class OrderDao{}

  5)測試和結果:

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        Dao bean = (Dao) context.getBean("indexDao");
        bean.query();
        System.out.println("----------");
        Dao dao = (Dao) context.getBean("orderDao");
        dao.query();
    }
    =========結果==========
    LogStart...
    query:IndexDao
    ----------
    query:IndexDao

   根據前文Spring筆記(3) - SpringAOP基礎詳解和原始碼探究可以得知如下:

  • @EnableAspectJAutoProxy(proxyTargetClass=false),表示使用JDK動態代理(預設);
  • @EnableAspectJAutoProxy(proxyTargetClass=true),表示使用CGLIB代理;

   debug執行後檢視beanFactory的OrderDao和IndexDao代理後的物件,如下圖:

 

   從上面可以看到IndexDao的物件是使用JDK代理的,而OrderDao的物件是CGLIB代理的。

  那麼當設定@EnableAspectJAutoProxy(proxyTargetClass=true)時,是如下圖:

 

   由此可以判定@DeclareParents註解給攔截到的類進行代理使用的是CGLIB代理(其實OrderDao沒有實現任何介面,所以最終實現的是CGLIB),那麼可以檢視下代理類生成的內容是什麼,可以在context前面加一行程式碼將代理類class檔案存入本地磁碟:

        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "xxx");//xxx是存放代理類class的本地路徑
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

 

   下面是代理類class反編譯後的完整程式碼(感興趣可以完全開啟,完全的很多內容,包含了各種父類的方法,比如toString、clone、equals、hashCode):

Spring(11) - Introductions進行類擴充套件方法
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.hrh.aop;

import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.SpringProxy;
import org.springframework.aop.TargetClassAware;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.AopConfigException;
import org.springframework.cglib.core.ReflectUtils;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Dispatcher;
import org.springframework.cglib.proxy.Factory;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.cglib.proxy.NoOp;

public class OrderDao$$EnhancerBySpringCGLIB$$ee33136 extends OrderDao implements Dao, SpringProxy, Advised, Factory {
    private boolean CGLIB$BOUND;
    public static Object CGLIB$FACTORY_DATA;
    private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
    private static final Callback[] CGLIB$STATIC_CALLBACKS;
    private MethodInterceptor CGLIB$CALLBACK_0;
    private MethodInterceptor CGLIB$CALLBACK_1;
    private NoOp CGLIB$CALLBACK_2;
    private Dispatcher CGLIB$CALLBACK_3;
    private Dispatcher CGLIB$CALLBACK_4;
    private MethodInterceptor CGLIB$CALLBACK_5;
    private MethodInterceptor CGLIB$CALLBACK_6;
    private static Object CGLIB$CALLBACK_FILTER;
    private static final Method CGLIB$equals$0$Method;
    private static final MethodProxy CGLIB$equals$0$Proxy;
    private static final Object[] CGLIB$emptyArgs;
    private static final Method CGLIB$toString$1$Method;
    private static final MethodProxy CGLIB$toString$1$Proxy;
    private static final Method CGLIB$hashCode$2$Method;
    private static final MethodProxy CGLIB$hashCode$2$Proxy;
    private static final Method CGLIB$clone$3$Method;
    private static final MethodProxy CGLIB$clone$3$Proxy;
    private static final Method CGLIB$query$4$Method;
    private static final MethodProxy CGLIB$query$4$Proxy;

    static void CGLIB$STATICHOOK5() {
        CGLIB$THREAD_CALLBACKS = new ThreadLocal();
        CGLIB$emptyArgs = new Object[0];
        Class var0 = Class.forName("com.hrh.aop.OrderDao$$EnhancerBySpringCGLIB$$ee33136");
        Class var1;
        Method[] var10000 = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods());
        CGLIB$equals$0$Method = var10000[0];
        CGLIB$equals$0$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$0");
        CGLIB$toString$1$Method = var10000[1];
        CGLIB$toString$1$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$1");
        CGLIB$hashCode$2$Method = var10000[2];
        CGLIB$hashCode$2$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$2");
        CGLIB$clone$3$Method = var10000[3];
        CGLIB$clone$3$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$3");
        CGLIB$query$4$Method = ReflectUtils.findMethods(new String[]{"query", "()V"}, (var1 = Class.forName("com.hrh.aop.Dao")).getDeclaredMethods())[0];
        CGLIB$query$4$Proxy = MethodProxy.create(var1, var0, "()V", "query", "CGLIB$query$4");
    }

    final boolean CGLIB$equals$0(Object var1) {
        return super.equals(var1);
    }

    public final boolean equals(Object var1) {
        try {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_5;
            if (this.CGLIB$CALLBACK_5 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_5;
            }

            if (var10000 != null) {
                Object var4 = var10000.intercept(this, CGLIB$equals$0$Method, new Object[]{var1}, CGLIB$equals$0$Proxy);
                return var4 == null ? false : (Boolean)var4;
            } else {
                return super.equals(var1);
            }
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    final String CGLIB$toString$1() {
        return super.toString();
    }

    public final String toString() {
        try {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }

            return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$1$Method, CGLIB$emptyArgs, CGLIB$toString$1$Proxy) : super.toString();
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    final int CGLIB$hashCode$2() {
        return super.hashCode();
    }

    public final int hashCode() {
        try {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_6;
            if (this.CGLIB$CALLBACK_6 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_6;
            }

            if (var10000 != null) {
                Object var3 = var10000.intercept(this, CGLIB$hashCode$2$Method, CGLIB$emptyArgs, CGLIB$hashCode$2$Proxy);
                return var3 == null ? 0 : ((Number)var3).intValue();
            } else {
                return super.hashCode();
            }
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    final Object CGLIB$clone$3() throws CloneNotSupportedException {
        return super.clone();
    }

    protected final Object clone() throws CloneNotSupportedException {
        try {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }

            return var10000 != null ? var10000.intercept(this, CGLIB$clone$3$Method, CGLIB$emptyArgs, CGLIB$clone$3$Proxy) : super.clone();
        } catch (Error | CloneNotSupportedException | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    final void CGLIB$query$4() {
        super.query();
    }

    public final void query() {
        try {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }

            if (var10000 != null) {
                var10000.intercept(this, CGLIB$query$4$Method, CGLIB$emptyArgs, CGLIB$query$4$Proxy);
            } else {
                super.query();
            }
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
        String var10000 = var0.toString();
        switch(var10000.hashCode()) {
        case -1166709331:
            if (var10000.equals("query()V")) {
                return CGLIB$query$4$Proxy;
            }
            break;
        case -508378822:
            if (var10000.equals("clone()Ljava/lang/Object;")) {
                return CGLIB$clone$3$Proxy;
            }
            break;
        case 1826985398:
            if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
                return CGLIB$equals$0$Proxy;
            }
            break;
        case 1913648695:
            if (var10000.equals("toString()Ljava/lang/String;")) {
                return CGLIB$toString$1$Proxy;
            }
            break;
        case 1984935277:
            if (var10000.equals("hashCode()I")) {
                return CGLIB$hashCode$2$Proxy;
            }
        }

        return null;
    }

    public final int indexOf(Advice var1) {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).indexOf(var1);
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final int indexOf(Advisor var1) {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).indexOf(var1);
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final boolean isFrozen() {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).isFrozen();
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public final void setTargetSource(TargetSource var1) {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            ((Advised)var10000.loadObject()).setTargetSource(var1);
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final Advisor[] getAdvisors() {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).getAdvisors();
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public final boolean isProxyTargetClass() {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).isProxyTargetClass();
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public final void addAdvice(Advice var1) throws AopConfigException {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            ((Advised)var10000.loadObject()).addAdvice(var1);
        } catch (Error | AopConfigException | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final void addAdvice(int var1, Advice var2) throws AopConfigException {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            ((Advised)var10000.loadObject()).addAdvice(var1, var2);
        } catch (Error | AopConfigException | RuntimeException var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public final boolean replaceAdvisor(Advisor var1, Advisor var2) throws AopConfigException {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).replaceAdvisor(var1, var2);
        } catch (Error | AopConfigException | RuntimeException var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public final void addAdvisor(int var1, Advisor var2) throws AopConfigException {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            ((Advised)var10000.loadObject()).addAdvisor(var1, var2);
        } catch (Error | AopConfigException | RuntimeException var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public final void addAdvisor(Advisor var1) throws AopConfigException {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            ((Advised)var10000.loadObject()).addAdvisor(var1);
        } catch (Error | AopConfigException | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final boolean isPreFiltered() {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).isPreFiltered();
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public final boolean removeAdvice(Advice var1) {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).removeAdvice(var1);
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final void removeAdvisor(int var1) throws AopConfigException {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            ((Advised)var10000.loadObject()).removeAdvisor(var1);
        } catch (Error | AopConfigException | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final boolean removeAdvisor(Advisor var1) {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).removeAdvisor(var1);
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final TargetSource getTargetSource() {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).getTargetSource();
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public final void setPreFiltered(boolean var1) {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            ((Advised)var10000.loadObject()).setPreFiltered(var1);
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final boolean isExposeProxy() {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).isExposeProxy();
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public final void setExposeProxy(boolean var1) {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            ((Advised)var10000.loadObject()).setExposeProxy(var1);
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final Class[] getProxiedInterfaces() {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).getProxiedInterfaces();
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public final boolean isInterfaceProxied(Class var1) {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).isInterfaceProxied(var1);
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final String toProxyConfigString() {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((Advised)var10000.loadObject()).toProxyConfigString();
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public final Class getTargetClass() {
        try {
            Dispatcher var10000 = this.CGLIB$CALLBACK_4;
            if (this.CGLIB$CALLBACK_4 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_4;
            }

            return ((TargetClassAware)var10000.loadObject()).getTargetClass();
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public OrderDao$$EnhancerBySpringCGLIB$$ee33136() {
        try {
            super();
            CGLIB$BIND_CALLBACKS(this);
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {
        CGLIB$THREAD_CALLBACKS.set(var0);
    }

    public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {
        CGLIB$STATIC_CALLBACKS = var0;
    }

    private static final void CGLIB$BIND_CALLBACKS(Object var0) {
        OrderDao$$EnhancerBySpringCGLIB$$ee33136 var1 = (OrderDao$$EnhancerBySpringCGLIB$$ee33136)var0;
        if (!var1.CGLIB$BOUND) {
            var1.CGLIB$BOUND = true;
            Object var10000 = CGLIB$THREAD_CALLBACKS.get();
            if (var10000 == null) {
                var10000 = CGLIB$STATIC_CALLBACKS;
                if (CGLIB$STATIC_CALLBACKS == null) {
                    return;
                }
            }

            Callback[] var10001 = (Callback[])var10000;
            var1.CGLIB$CALLBACK_6 = (MethodInterceptor)((Callback[])var10000)[6];
            var1.CGLIB$CALLBACK_5 = (MethodInterceptor)var10001[5];
            var1.CGLIB$CALLBACK_4 = (Dispatcher)var10001[4];
            var1.CGLIB$CALLBACK_3 = (Dispatcher)var10001[3];
            var1.CGLIB$CALLBACK_2 = (NoOp)var10001[2];
            var1.CGLIB$CALLBACK_1 = (MethodInterceptor)var10001[1];
            var1.CGLIB$CALLBACK_0 = (MethodInterceptor)var10001[0];
        }

    }

    public Object newInstance(Callback[] var1) {
        try {
            CGLIB$SET_THREAD_CALLBACKS(var1);
            OrderDao$$EnhancerBySpringCGLIB$$ee33136 var10000 = new OrderDao$$EnhancerBySpringCGLIB$$ee33136();
            CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
            return var10000;
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public Object newInstance(Callback var1) {
        try {
            throw new IllegalStateException("More than one callback object required");
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
        try {
            CGLIB$SET_THREAD_CALLBACKS(var3);
            OrderDao$$EnhancerBySpringCGLIB$$ee33136 var10000 = new OrderDao$$EnhancerBySpringCGLIB$$ee33136;
            switch(var1.length) {
            case 0:
                var10000.<init>();
                CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
                return var10000;
            default:
                throw new IllegalArgumentException("Constructor not found");
            }
        } catch (Error | RuntimeException var4) {
            throw var4;
        } catch (Throwable var5) {
            throw new UndeclaredThrowableException(var5);
        }
    }

    public Callback getCallback(int var1) {
        try {
            CGLIB$BIND_CALLBACKS(this);
            Object var10000;
            switch(var1) {
            case 0:
                var10000 = this.CGLIB$CALLBACK_0;
                break;
            case 1:
                var10000 = this.CGLIB$CALLBACK_1;
                break;
            case 2:
                var10000 = this.CGLIB$CALLBACK_2;
                break;
            case 3:
                var10000 = this.CGLIB$CALLBACK_3;
                break;
            case 4:
                var10000 = this.CGLIB$CALLBACK_4;
                break;
            case 5:
                var10000 = this.CGLIB$CALLBACK_5;
                break;
            case 6:
                var10000 = this.CGLIB$CALLBACK_6;
                break;
            default:
                var10000 = null;
            }

            return (Callback)var10000;
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public void setCallback(int var1, Callback var2) {
        try {
            switch(var1) {
            case 0:
                this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
                break;
            case 1:
                this.CGLIB$CALLBACK_1 = (MethodInterceptor)var2;
                break;
            case 2:
                this.CGLIB$CALLBACK_2 = (NoOp)var2;
                break;
            case 3:
                this.CGLIB$CALLBACK_3 = (Dispatcher)var2;
                break;
            case 4:
                this.CGLIB$CALLBACK_4 = (Dispatcher)var2;
                break;
            case 5:
                this.CGLIB$CALLBACK_5 = (MethodInterceptor)var2;
                break;
            case 6:
                this.CGLIB$CALLBACK_6 = (MethodInterceptor)var2;
            }

        } catch (Error | RuntimeException var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public Callback[] getCallbacks() {
        try {
            CGLIB$BIND_CALLBACKS(this);
            return new Callback[]{this.CGLIB$CALLBACK_0, this.CGLIB$CALLBACK_1, this.CGLIB$CALLBACK_2, this.CGLIB$CALLBACK_3, this.CGLIB$CALLBACK_4, this.CGLIB$CALLBACK_5, this.CGLIB$CALLBACK_6};
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }

    public void setCallbacks(Callback[] var1) {
        try {
            this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];
            this.CGLIB$CALLBACK_1 = (MethodInterceptor)var1[1];
            this.CGLIB$CALLBACK_2 = (NoOp)var1[2];
            this.CGLIB$CALLBACK_3 = (Dispatcher)var1[3];
            this.CGLIB$CALLBACK_4 = (Dispatcher)var1[4];
            this.CGLIB$CALLBACK_5 = (MethodInterceptor)var1[5];
            this.CGLIB$CALLBACK_6 = (MethodInterceptor)var1[6];
        } catch (Error | RuntimeException var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    static {
        CGLIB$STATICHOOK6();
        CGLIB$STATICHOOK5();
    }

    static void CGLIB$STATICHOOK6() {
        try {
            ;
        } catch (Error | RuntimeException var0) {
            throw var0;
        } catch (Throwable var1) {
            throw new UndeclaredThrowableException(var1);
        }
    }
}
View Code

  從上面可以看到代理類裡面有query方法,代理類繼承了OrderDao和實現了Dao介面:

public class OrderDao$$EnhancerBySpringCGLIB$$ee33136 extends OrderDao implements Dao, SpringProxy, Advised, Factory {    
    ............................
    
    final void CGLIB$query$4() {
        super.query();
    }

    public final void query() {
        try {
            MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
            if (this.CGLIB$CALLBACK_0 == null) {
                CGLIB$BIND_CALLBACKS(this);
                var10000 = this.CGLIB$CALLBACK_0;
            }

            if (var10000 != null) {
                var10000.intercept(this, CGLIB$query$4$Method, CGLIB$emptyArgs, CGLIB$query$4$Proxy);
            } else {
                super.query();
            }
        } catch (Error | RuntimeException var1) {
            throw var1;
        } catch (Throwable var2) {
            throw new UndeclaredThrowableException(var2);
        }
    }
    
    ............................
}    

   從前文Spring筆記(3) - SpringAOP基礎詳解和原始碼探究可以得知在 AbstractAutoProxyCreator#wrapIfNecessary()中會對bean進行包裝增強(代理),下面打下斷點debug跟蹤下流程:

 

 

   當執行到wrapIfNecessary方法下面的程式碼時,bean還是原物件(未被代理):

Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);

 

   當上面的程式碼執行完後,可以看到specificInterceptors陣列包含了advice(通知)、introducedInterface(引用介面)、typePatternClassFilter(class過濾規則,重點),其中advice包含了defaultImplType(預設實現型別,上面程式碼中定義的),可以看到是IndexDao,interfaceType(介面型別)可以看到是Dao,以上資訊是通過解析@DeclareParents註解而來的:

   下面typePatternClassFilter裡面的詳情,可以看到,上面的資訊又包含在typePatternClassFilter裡面,同時裡面包含了過濾規則:

   最後通過DefaultAopProxyFactory#createAopProxy方法建立的物件如下圖:

   當執行query方法時,CglibAopProxy.DynamicAdvisedInterceptor#intercept方法進行攔截時,會執行父類的方法ReflectiveMethodInvocation#ReflectiveMethodInvocation:

 

   執行完會跳轉到ReflectiveMethodInvocation#proceed執行方法的執行:

    public Object proceed() throws Throwable {
        // We start with an index of -1 and increment early.
        if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
            return invokeJoinpoint();
        }
        //獲取通知
        Object interceptorOrInterceptionAdvice =
                this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
        if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
            // Evaluate dynamic method matcher here: static part will already have
            // been evaluated and found to match.
            InterceptorAndDynamicMethodMatcher dm =
                    (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
            if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
                return dm.interceptor.invoke(this);
            }
            else {
                // Dynamic matching failed.
                // Skip this interceptor and invoke the next in the chain.
                return proceed();
            }
        }
        else {
            // It's an interceptor, so we just invoke it: The pointcut will have
            // been evaluated statically before this object was constructed.
            return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);//執行方法
        }
    }    

 

  下面執行delegate物件獲取到IndexDao,然後AopUtils.invokeJoinpointUsingReflection執行IndexDao的query實現方法:

  其中getIntroductionDelegateFor方法中的delegate通過反射對預設實現型別進行構造方法例項化後呼叫例項化的方法:

 

 

   總結:Introductions(引用)通過 @DeclareParents 註解實現,可以給一個空白類進行方法複製,它通過解析@DeclareParents 註解資訊,代理時將定義的靜態介面變數作為它的父類,並實現它的方法,方法執行時呼叫的是defaultImpl 指定的類别範本的實現方法;

相關文章