ITPub博客

首页 > 数据库 > 数据库开发技术 > Spring-14 AOP CGLIB 代理对象源码

Spring-14 AOP CGLIB 代理对象源码

原创 数据库开发技术 作者:DevXTalk 时间:2023-03-27 21:37:46 0 删除 编辑

Spring-14 AOP CGLIB 代理对象源码

Spring 源码系列文章会遵循由浅入深,由易到难,由宏观到微观的原则,目标是尽量降低学习难度,而不是一上来就迷失在源码当中. 文章会从一个场景作为出发点,针对性的目的性极强的针对该场景对 Spring 的实现原理,源码进行探究学习。该系列文章会让你收获什么? 从对 Spring 的使用者成为 Spring 专家。该文章会同步在微信公众号 【DevXJava】, 方便在微信客户端阅读。

本章会介绍 AOP 生成的代理对象源码,让读者更加清晰的认识 spring AOP 创建的代理对象。

场景
public class AopExp1 {

public static void main(String[] args) throws Exception {

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger logger = loggerContext.getLogger(AnnotationAwareAspectJAutoProxyCreator.class);
logger.setLevel(Level.TRACE);

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();

AnnotationAwareAspectJAutoProxyCreator proxyCreator = new AnnotationAwareAspectJAutoProxyCreator();
proxyCreator.setBeanFactory(factory);
factory.addBeanPostProcessor(proxyCreator);

factory.registerBeanDefinition("myAspect" , BeanDefinitionBuilder
.genericBeanDefinition(MyAspect.class)
.getBeanDefinition());

factory.registerBeanDefinition("bean1" , BeanDefinitionBuilder
.genericBeanDefinition(Bean1.class)
.getBeanDefinition());

Bean1 bean1 = factory.getBean(Bean1.class);
System.out.println("bean1 type -> " + bean1.getClass());
bean1.foo();

System.in.read();
}


@Aspect
static class MyAspect {

@Before("execution(* foo())")
public void before() {
System.out.println("@Aspect @Before ------------------------------");
}

}

static class Bean1 {


public Bean1() {
System.out.println("======================== bean1 实例化 ");
}

public void foo() {
System.out.println("======================== Bean1 foo ");
}
}
}
日志输出
======================== bean1 实例化 
21:16:30.709 [main] TRACE org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator - Creating implicit proxy for bean 'bean1' with 0 common interceptors and 2 specific interceptors
bean1 type -> class org.devx.spring.certified.professional.edu.hp.experiment.AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478
21:16:30.833 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myAspect'
@Aspect @Before ------------------------------
======================== Bean1 foo

从日志中可以看到这是生成的代理对象类名 org.devx.spring.certified.professional.edu.hp.experiment.AopExp1$3f1c0478

CGLIB 生成代理对象源码
/*
* Decompiled with CFR.
*/
package org.devx.spring.certified.professional.edu.hp.experiment;

import java.lang.reflect.Method;
import org.aopalliance.aop.Advice;
import org.devx.spring.certified.professional.edu.hp.experiment.AopExp1;
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 AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478
extends AopExp1.Bean1
implements 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$foo$0$Method;
private static final MethodProxy CGLIB$foo$0$Proxy;
private static final Object[] CGLIB$emptyArgs;
private static final Method CGLIB$equals$1$Method;
private static final MethodProxy CGLIB$equals$1$Proxy;
private static final Method CGLIB$toString$2$Method;
private static final MethodProxy CGLIB$toString$2$Proxy;
private static final Method CGLIB$hashCode$3$Method;
private static final MethodProxy CGLIB$hashCode$3$Proxy;
private static final Method CGLIB$clone$4$Method;
private static final MethodProxy CGLIB$clone$4$Proxy;

static void CGLIB$STATICHOOK1() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
Class<?> clazz = Class.forName("org.devx.spring.certified.professional.edu.hp.experiment.AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478");
Class<?> clazz2 = Class.forName("java.lang.Object");
Method[] methodArray = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, clazz2.getDeclaredMethods());
CGLIB$equals$1$Method = methodArray[0];
CGLIB$equals$1$Proxy = MethodProxy.create(clazz2, clazz, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$1");
CGLIB$toString$2$Method = methodArray[1];
CGLIB$toString$2$Proxy = MethodProxy.create(clazz2, clazz, "()Ljava/lang/String;", "toString", "CGLIB$toString$2");
CGLIB$hashCode$3$Method = methodArray[2];
CGLIB$hashCode$3$Proxy = MethodProxy.create(clazz2, clazz, "()I", "hashCode", "CGLIB$hashCode$3");
CGLIB$clone$4$Method = methodArray[3];
CGLIB$clone$4$Proxy = MethodProxy.create(clazz2, clazz, "()Ljava/lang/Object;", "clone", "CGLIB$clone$4");
clazz2 = Class.forName("org.devx.spring.certified.professional.edu.hp.experiment.AopExp1$Bean1");
CGLIB$foo$0$Method = ReflectUtils.findMethods(new String[]{"foo", "()V"}, clazz2.getDeclaredMethods())[0];
CGLIB$foo$0$Proxy = MethodProxy.create(clazz2, clazz, "()V", "foo", "CGLIB$foo$0");
}

final void CGLIB$foo$0() {
super.foo();
}

@Override
public final void foo() {
MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
if (methodInterceptor == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
methodInterceptor = this.CGLIB$CALLBACK_0;
}
if (methodInterceptor != null) {
Object object = methodInterceptor.intercept(this, CGLIB$foo$0$Method, CGLIB$emptyArgs, CGLIB$foo$0$Proxy);
return;
}
super.foo();
}

final boolean CGLIB$equals$1(Object object) {
return super.equals(object);
}

public final boolean equals(Object object) {
MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_5;
if (methodInterceptor == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
methodInterceptor = this.CGLIB$CALLBACK_5;
}
if (methodInterceptor != null) {
Object object2 = methodInterceptor.intercept(this, CGLIB$equals$1$Method, new Object[]{object}, CGLIB$equals$1$Proxy);
return object2 == null ? false : (Boolean)object2;
}
return super.equals(object);
}

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

public final String toString() {
MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
if (methodInterceptor == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
methodInterceptor = this.CGLIB$CALLBACK_0;
}
if (methodInterceptor != null) {
return (String)methodInterceptor.intercept(this, CGLIB$toString$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy);
}
return super.toString();
}

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

public final int hashCode() {
MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_6;
if (methodInterceptor == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
methodInterceptor = this.CGLIB$CALLBACK_6;
}
if (methodInterceptor != null) {
Object object = methodInterceptor.intercept(this, CGLIB$hashCode$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy);
return object == null ? 0 : ((Number)object).intValue();
}
return super.hashCode();
}

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

protected final Object clone() throws CloneNotSupportedException {
MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
if (methodInterceptor == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
methodInterceptor = this.CGLIB$CALLBACK_0;
}
if (methodInterceptor != null) {
return methodInterceptor.intercept(this, CGLIB$clone$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy);
}
return super.clone();
}

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

@Override
public final int indexOf(Advisor advisor) {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).indexOf(advisor);
}

@Override
public final int indexOf(Advice advice) {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).indexOf(advice);
}

@Override
public final boolean isFrozen() {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).isFrozen();
}

public final boolean isInterfaceProxied(Class clazz) {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).isInterfaceProxied(clazz);
}

public final Class[] getProxiedInterfaces() {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).getProxiedInterfaces();
}

@Override
public final String toProxyConfigString() {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).toProxyConfigString();
}

@Override
public final boolean isProxyTargetClass() {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).isProxyTargetClass();
}

@Override
public final void setTargetSource(TargetSource targetSource) {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
((Advised)dispatcher.loadObject()).setTargetSource(targetSource);
}

@Override
public final TargetSource getTargetSource() {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).getTargetSource();
}

@Override
public final void setPreFiltered(boolean bl) {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
((Advised)dispatcher.loadObject()).setPreFiltered(bl);
}

@Override
public final boolean isExposeProxy() {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).isExposeProxy();
}

@Override
public final void setExposeProxy(boolean bl) {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
((Advised)dispatcher.loadObject()).setExposeProxy(bl);
}

@Override
public final Advisor[] getAdvisors() {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).getAdvisors();
}

@Override
public final int getAdvisorCount() {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).getAdvisorCount();
}

@Override
public final void addAdvisor(Advisor advisor) throws AopConfigException {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
((Advised)dispatcher.loadObject()).addAdvisor(advisor);
}

@Override
public final void addAdvisor(int n, Advisor advisor) throws AopConfigException {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
((Advised)dispatcher.loadObject()).addAdvisor(n, advisor);
}

@Override
public final boolean removeAdvice(Advice advice) {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).removeAdvice(advice);
}

@Override
public final void addAdvice(Advice advice) throws AopConfigException {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
((Advised)dispatcher.loadObject()).addAdvice(advice);
}

@Override
public final void addAdvice(int n, Advice advice) throws AopConfigException {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
((Advised)dispatcher.loadObject()).addAdvice(n, advice);
}

@Override
public final boolean removeAdvisor(Advisor advisor) {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).removeAdvisor(advisor);
}

@Override
public final void removeAdvisor(int n) throws AopConfigException {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
((Advised)dispatcher.loadObject()).removeAdvisor(n);
}

@Override
public final boolean replaceAdvisor(Advisor advisor, Advisor advisor2) throws AopConfigException {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).replaceAdvisor(advisor, advisor2);
}

@Override
public final boolean isPreFiltered() {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((Advised)dispatcher.loadObject()).isPreFiltered();
}

public final Class getTargetClass() {
Dispatcher dispatcher = this.CGLIB$CALLBACK_4;
if (dispatcher == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
dispatcher = this.CGLIB$CALLBACK_4;
}
return ((TargetClassAware)dispatcher.loadObject()).getTargetClass();
}

public AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478() {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 = this;
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478);
}

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

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

private static final void CGLIB$BIND_CALLBACKS(Object object) {
block2: {
Object object2;
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478;
block3: {
aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 = (AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478)object;
if (aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BOUND) break block2;
aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BOUND = true;
object2 = CGLIB$THREAD_CALLBACKS.get();
if (object2 != null) break block3;
object2 = CGLIB$STATIC_CALLBACKS;
if (CGLIB$STATIC_CALLBACKS == null) break block2;
}
Callback[] callbackArray = (Callback[])object2;
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c04782 = aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478;
aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c04782.CGLIB$CALLBACK_6 = (MethodInterceptor)callbackArray[6];
aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c04782.CGLIB$CALLBACK_5 = (MethodInterceptor)callbackArray[5];
aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c04782.CGLIB$CALLBACK_4 = (Dispatcher)callbackArray[4];
aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c04782.CGLIB$CALLBACK_3 = (Dispatcher)callbackArray[3];
aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c04782.CGLIB$CALLBACK_2 = (NoOp)callbackArray[2];
aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c04782.CGLIB$CALLBACK_1 = (MethodInterceptor)callbackArray[1];
aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c04782.CGLIB$CALLBACK_0 = (MethodInterceptor)callbackArray[0];
}
}

@Override
public Object newInstance(Callback[] callbackArray) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$SET_THREAD_CALLBACKS(callbackArray);
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 = new AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478();
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$SET_THREAD_CALLBACKS(null);
return aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478;
}

@Override
public Object newInstance(Callback callback) {
throw new IllegalStateException("More than one callback object required");
}

@Override
public Object newInstance(Class[] classArray, Object[] objectArray, Callback[] callbackArray) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478;
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$SET_THREAD_CALLBACKS(callbackArray);
Class[] classArray2 = classArray;
switch (classArray.length) {
case 0: {
aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 = new AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478();
break;
}
default: {
throw new IllegalArgumentException("Constructor not found");
}
}
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$SET_THREAD_CALLBACKS(null);
return aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478;
}

@Override
public Callback getCallback(int n) {
Callback callback;
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 = this;
switch (n) {
case 0: {
callback = aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$CALLBACK_0;
break;
}
case 1: {
callback = aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$CALLBACK_1;
break;
}
case 2: {
callback = aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$CALLBACK_2;
break;
}
case 3: {
callback = aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$CALLBACK_3;
break;
}
case 4: {
callback = aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$CALLBACK_4;
break;
}
case 5: {
callback = aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$CALLBACK_5;
break;
}
case 6: {
callback = aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$CALLBACK_6;
break;
}
default: {
callback = null;
}
}
return callback;
}

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

@Override
public Callback[] getCallbacks() {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$BIND_CALLBACKS(this);
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 = 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};
}

@Override
public void setCallbacks(Callback[] callbackArray) {
this.CGLIB$CALLBACK_0 = (MethodInterceptor)callbackArray[0];
this.CGLIB$CALLBACK_1 = (MethodInterceptor)callbackArray[1];
this.CGLIB$CALLBACK_2 = (NoOp)callbackArray[2];
this.CGLIB$CALLBACK_3 = (Dispatcher)callbackArray[3];
this.CGLIB$CALLBACK_4 = (Dispatcher)callbackArray[4];
this.CGLIB$CALLBACK_5 = (MethodInterceptor)callbackArray[5];
Callback[] callbackArray2 = callbackArray;
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 aopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478 = this;
this.CGLIB$CALLBACK_6 = (MethodInterceptor)callbackArray[6];
}

static {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$3f1c0478.CGLIB$STATICHOOK1();
}
}

重点看 foo() 方法, 再调用了 MethodInterceptor 的 intercept 后会调用父类的 foo() 方法。

public final void foo() {
MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
if (methodInterceptor == null) {
AopExp1$Bean1$$EnhancerBySpringCGLIB$$5974b8b5.CGLIB$BIND_CALLBACKS(this);
methodInterceptor = this.CGLIB$CALLBACK_0;
}
if (methodInterceptor != null) {
Object object = methodInterceptor.intercept(this, CGLIB$foo$0$Method, CGLIB$emptyArgs, CGLIB$foo$0$Proxy);
return;
}
super.foo();
}

再结合 spring 创建 CGLIB 代理时的代码来看 this.CGLIB$CALLBACK_0 对应的就是 DynamicAdvisedInterceptor 。

org.springframework.aop.framework.CglibAopProxy#getCallbacks

private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
// Parameters used for optimization choices...
boolean exposeProxy = this.advised.isExposeProxy();
boolean isFrozen = this.advised.isFrozen();
boolean isStatic = this.advised.getTargetSource().isStatic();

// Choose an "aop" interceptor (used for AOP calls).
Callback aopInterceptor = new DynamicAdvisedInterceptor(this.advised);

// Choose a "straight to target" interceptor. (used for calls that are
// unadvised but can return this). May be required to expose the proxy.
Callback targetInterceptor;
if (exposeProxy) {
targetInterceptor = (isStatic ?
new StaticUnadvisedExposedInterceptor(this.advised.getTargetSource().getTarget()) :
new DynamicUnadvisedExposedInterceptor(this.advised.getTargetSource()));
}
else {
targetInterceptor = (isStatic ?
new StaticUnadvisedInterceptor(this.advised.getTargetSource().getTarget()) :
new DynamicUnadvisedInterceptor(this.advised.getTargetSource()));
}

// Choose a "direct to target" dispatcher (used for
// unadvised calls to static targets that cannot return this).
Callback targetDispatcher = (isStatic ?
new StaticDispatcher(this.advised.getTargetSource().getTarget()) : new SerializableNoOp());

Callback[] mainCallbacks = new Callback[] {
aopInterceptor, // for normal advice
targetInterceptor, // invoke target without considering advice, if optimized
new SerializableNoOp(), // no override for methods mapped to this
targetDispatcher, this.advisedDispatcher,
new EqualsInterceptor(this.advised),
new HashCodeInterceptor(this.advised)
};

Callback[] callbacks;

// If the target is a static one and the advice chain is frozen,
// then we can make some optimizations by sending the AOP calls
// direct to the target using the fixed chain for that method.
if (isStatic && isFrozen) {
Method[] methods = rootClass.getMethods();
Callback[] fixedCallbacks = new Callback[methods.length];
this.fixedInterceptorMap = CollectionUtils.newHashMap(methods.length);

// TODO: small memory optimization here (can skip creation for methods with no advice)
for (int x = 0; x < methods.length; x++) {
Method method = methods[x];
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, rootClass);
fixedCallbacks[x] = new FixedChainStaticTargetInterceptor(
chain, this.advised.getTargetSource().getTarget(), this.advised.getTargetClass());
this.fixedInterceptorMap.put(method, x);
}

// Now copy both the callbacks from mainCallbacks
// and fixedCallbacks into the callbacks array.
callbacks = new Callback[mainCallbacks.length + fixedCallbacks.length];
System.arraycopy(mainCallbacks, 0, callbacks, 0, mainCallbacks.length);
System.arraycopy(fixedCallbacks, 0, callbacks, mainCallbacks.length, fixedCallbacks.length);
this.fixedInterceptorOffset = mainCallbacks.length;
}
else {
callbacks = mainCallbacks;
}
return callbacks;
}

DevX 会持续分享 Java 技术干货,如果你觉得本文对你有帮助希望你可以分享给更多的朋友看到。该文章会同步在微信公众号 【DevXJava】, 方便在微信客户端阅读。

DevX 不止于技术


原文作者:DevXTalk;如有侵权,请联系管理员删除。

上一篇: 什么是 Web3 ?
请登录后发表评论 登录
全部评论

注册时间:2019-03-23

  • 博文量
    178
  • 访问量
    82612