publicclassWorldServiceInterceptorimplementsMethodInterceptor{@OverridepublicObjectinvoke(MethodInvocationmethodInvocation)throwsThrowable{System.out.println("do something before the Earth explodes");Objectres=methodInvocation.proceed();System.out.println("do something after the Earth explodes");returnres;}}
/* * Decompiled with CFR. * * Could not load the following classes: * test.aop.bean.WorldService */packagecom.sun.proxy;importjava.lang.reflect.InvocationHandler;importjava.lang.reflect.Method;importjava.lang.reflect.Proxy;importjava.lang.reflect.UndeclaredThrowableException;importtest.aop.bean.WorldService;publicfinalclass$Proxy2extendsProxyimplementsWorldService{privatestaticMethodm1;privatestaticMethodm2;privatestaticMethodm0;privatestaticMethodm3;public$Proxy2(InvocationHandlerinvocationHandler){super(invocationHandler);}static{try{m1=Class.forName("java.lang.Object").getMethod("equals",Class.forName("java.lang.Object"));m2=Class.forName("java.lang.Object").getMethod("toString",newClass[0]);m0=Class.forName("java.lang.Object").getMethod("hashCode",newClass[0]);m3=Class.forName("test.aop.bean.WorldService").getMethod("explode",newClass[0]);return;}catch(NoSuchMethodExceptionnoSuchMethodException){thrownewNoSuchMethodError(noSuchMethodException.getMessage());}catch(ClassNotFoundExceptionclassNotFoundException){thrownewNoClassDefFoundError(classNotFoundException.getMessage());}}publicfinalbooleanequals(Objectobject){try{return(Boolean)this.h.invoke(this,m1,newObject[]{object});}catch(Error|RuntimeExceptionthrowable){throwthrowable;}catch(Throwablethrowable){thrownewUndeclaredThrowableException(throwable);}}publicfinalStringtoString(){try{return(String)this.h.invoke(this,m2,null);}catch(Error|RuntimeExceptionthrowable){throwthrowable;}catch(Throwablethrowable){thrownewUndeclaredThrowableException(throwable);}}publicfinalinthashCode(){try{return(Integer)this.h.invoke(this,m0,null);}catch(Error|RuntimeExceptionthrowable){throwthrowable;}catch(Throwablethrowable){thrownewUndeclaredThrowableException(throwable);}}publicfinalvoidexplode(){try{this.h.invoke(this,m3,null);return;}catch(Error|RuntimeExceptionthrowable){throwthrowable;}catch(Throwablethrowable){thrownewUndeclaredThrowableException(throwable);}}}
可以看到代理类信息如下 public final class $Proxy2 extends Proxy implements WorldService
/* * Decompiled with CFR. * * Could not load the following classes: * test.aop.bean.WorldService * test.aop.bean.WorldServiceImpl */packagetest.aop.bean;importjava.lang.reflect.Method;importnet.sf.cglib.core.ReflectUtils;importnet.sf.cglib.core.Signature;importnet.sf.cglib.proxy.Callback;importnet.sf.cglib.proxy.Factory;importnet.sf.cglib.proxy.MethodInterceptor;importnet.sf.cglib.proxy.MethodProxy;importtest.aop.bean.WorldService;importtest.aop.bean.WorldServiceImpl;publicclassWorldServiceImpl$$EnhancerByCGLIB$$81790592extendsWorldServiceImplimplementsWorldService,Factory{privatebooleanCGLIB$BOUND;publicstaticObjectCGLIB$FACTORY_DATA;privatestaticfinalThreadLocalCGLIB$THREAD_CALLBACKS;privatestaticfinalCallback[]CGLIB$STATIC_CALLBACKS;privateMethodInterceptorCGLIB$CALLBACK_0;privatestaticObjectCGLIB$CALLBACK_FILTER;privatestaticfinalMethodCGLIB$explode$0$Method;privatestaticfinalMethodProxyCGLIB$explode$0$Proxy;privatestaticfinalObject[]CGLIB$emptyArgs;privatestaticfinalMethodCGLIB$equals$1$Method;privatestaticfinalMethodProxyCGLIB$equals$1$Proxy;privatestaticfinalMethodCGLIB$toString$2$Method;privatestaticfinalMethodProxyCGLIB$toString$2$Proxy;privatestaticfinalMethodCGLIB$hashCode$3$Method;privatestaticfinalMethodProxyCGLIB$hashCode$3$Proxy;privatestaticfinalMethodCGLIB$clone$4$Method;privatestaticfinalMethodProxyCGLIB$clone$4$Proxy;publicWorldServiceImpl$$EnhancerByCGLIB$$81790592(){WorldServiceImpl$$EnhancerByCGLIB$$81790592worldServiceImpl$$EnhancerByCGLIB$$81790592=this;WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$BIND_CALLBACKS(worldServiceImpl$$EnhancerByCGLIB$$81790592);}static{WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$STATICHOOK1();}publicfinalbooleanequals(Objectobject){MethodInterceptormethodInterceptor=this.CGLIB$CALLBACK_0;if(methodInterceptor==null){WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$BIND_CALLBACKS(this);methodInterceptor=this.CGLIB$CALLBACK_0;}if(methodInterceptor!=null){Objectobject2=methodInterceptor.intercept(this,CGLIB$equals$1$Method,newObject[]{object},CGLIB$equals$1$Proxy);returnobject2==null?false:(Boolean)object2;}returnsuper.equals(object);}publicfinalStringtoString(){MethodInterceptormethodInterceptor=this.CGLIB$CALLBACK_0;if(methodInterceptor==null){WorldServiceImpl$$EnhancerByCGLIB$$81790592.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);}returnsuper.toString();}publicfinalinthashCode(){MethodInterceptormethodInterceptor=this.CGLIB$CALLBACK_0;if(methodInterceptor==null){WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$BIND_CALLBACKS(this);methodInterceptor=this.CGLIB$CALLBACK_0;}if(methodInterceptor!=null){Objectobject=methodInterceptor.intercept(this,CGLIB$hashCode$3$Method,CGLIB$emptyArgs,CGLIB$hashCode$3$Proxy);returnobject==null?0:((Number)object).intValue();}returnsuper.hashCode();}protectedfinalObjectclone()throwsCloneNotSupportedException{MethodInterceptormethodInterceptor=this.CGLIB$CALLBACK_0;if(methodInterceptor==null){WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$BIND_CALLBACKS(this);methodInterceptor=this.CGLIB$CALLBACK_0;}if(methodInterceptor!=null){returnmethodInterceptor.intercept(this,CGLIB$clone$4$Method,CGLIB$emptyArgs,CGLIB$clone$4$Proxy);}returnsuper.clone();}@OverridepublicObjectnewInstance(Callback[]callbackArray){WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$SET_THREAD_CALLBACKS(callbackArray);WorldServiceImpl$$EnhancerByCGLIB$$81790592worldServiceImpl$$EnhancerByCGLIB$$81790592=newWorldServiceImpl$$EnhancerByCGLIB$$81790592();WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$SET_THREAD_CALLBACKS(null);returnworldServiceImpl$$EnhancerByCGLIB$$81790592;}@OverridepublicObjectnewInstance(Callbackcallback){WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$SET_THREAD_CALLBACKS(newCallback[]{callback});WorldServiceImpl$$EnhancerByCGLIB$$81790592worldServiceImpl$$EnhancerByCGLIB$$81790592=newWorldServiceImpl$$EnhancerByCGLIB$$81790592();WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$SET_THREAD_CALLBACKS(null);returnworldServiceImpl$$EnhancerByCGLIB$$81790592;}@OverridepublicObjectnewInstance(Class[]classArray,Object[]objectArray,Callback[]callbackArray){WorldServiceImpl$$EnhancerByCGLIB$$81790592worldServiceImpl$$EnhancerByCGLIB$$81790592;WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$SET_THREAD_CALLBACKS(callbackArray);Class[]classArray2=classArray;switch(classArray.length){case0:{worldServiceImpl$$EnhancerByCGLIB$$81790592=newWorldServiceImpl$$EnhancerByCGLIB$$81790592();break;}default:{thrownewIllegalArgumentException("Constructor not found");}}WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$SET_THREAD_CALLBACKS(null);returnworldServiceImpl$$EnhancerByCGLIB$$81790592;}publicfinalvoidexplode(){MethodInterceptormethodInterceptor=this.CGLIB$CALLBACK_0;if(methodInterceptor==null){WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$BIND_CALLBACKS(this);methodInterceptor=this.CGLIB$CALLBACK_0;}if(methodInterceptor!=null){Objectobject=methodInterceptor.intercept(this,CGLIB$explode$0$Method,CGLIB$emptyArgs,CGLIB$explode$0$Proxy);return;}super.explode();}@OverridepublicCallback[]getCallbacks(){WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$BIND_CALLBACKS(this);WorldServiceImpl$$EnhancerByCGLIB$$81790592worldServiceImpl$$EnhancerByCGLIB$$81790592=this;returnnewCallback[]{this.CGLIB$CALLBACK_0};}staticvoidCGLIB$STATICHOOK1(){CGLIB$THREAD_CALLBACKS=newThreadLocal();CGLIB$emptyArgs=newObject[0];Class<?>clazz=Class.forName("test.aop.bean.WorldServiceImpl$$EnhancerByCGLIB$$81790592");Class<?>clazz2=Class.forName("test.aop.bean.WorldServiceImpl");CGLIB$explode$0$Method=ReflectUtils.findMethods(newString[]{"explode","()V"},clazz2.getDeclaredMethods())[0];CGLIB$explode$0$Proxy=MethodProxy.create(clazz2,clazz,"()V","explode","CGLIB$explode$0");clazz2=Class.forName("java.lang.Object");Method[]methodArray=ReflectUtils.findMethods(newString[]{"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");}finalStringCGLIB$toString$2(){returnsuper.toString();}privatestaticfinalvoidCGLIB$BIND_CALLBACKS(Objectobject){block2:{Objectobject2;block3:{WorldServiceImpl$$EnhancerByCGLIB$$81790592worldServiceImpl$$EnhancerByCGLIB$$81790592=(WorldServiceImpl$$EnhancerByCGLIB$$81790592)object;if(worldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$BOUND)breakblock2;worldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$BOUND=true;object2=CGLIB$THREAD_CALLBACKS.get();if(object2!=null)breakblock3;object2=CGLIB$STATIC_CALLBACKS;if(CGLIB$STATIC_CALLBACKS==null)breakblock2;}worldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$CALLBACK_0=(MethodInterceptor)((Callback[])object2)[0];}}finalbooleanCGLIB$equals$1(Objectobject){returnsuper.equals(object);}finalvoidCGLIB$explode$0(){super.explode();}finalintCGLIB$hashCode$3(){returnsuper.hashCode();}@OverridepublicvoidsetCallback(intn,Callbackcallback){switch(n){case0:{this.CGLIB$CALLBACK_0=(MethodInterceptor)callback;break;}}}@OverridepublicvoidsetCallbacks(Callback[]callbackArray){Callback[]callbackArray2=callbackArray;WorldServiceImpl$$EnhancerByCGLIB$$81790592worldServiceImpl$$EnhancerByCGLIB$$81790592=this;this.CGLIB$CALLBACK_0=(MethodInterceptor)callbackArray[0];}publicstaticMethodProxyCGLIB$findMethodProxy(Signaturesignature){Stringstring=((Object)signature).toString();switch(string.hashCode()){case-508378822:{if(!string.equals("clone()Ljava/lang/Object;"))break;returnCGLIB$clone$4$Proxy;}case1741417204:{if(!string.equals("explode()V"))break;returnCGLIB$explode$0$Proxy;}case1826985398:{if(!string.equals("equals(Ljava/lang/Object;)Z"))break;returnCGLIB$equals$1$Proxy;}case1913648695:{if(!string.equals("toString()Ljava/lang/String;"))break;returnCGLIB$toString$2$Proxy;}case1984935277:{if(!string.equals("hashCode()I"))break;returnCGLIB$hashCode$3$Proxy;}}returnnull;}finalObjectCGLIB$clone$4()throwsCloneNotSupportedException{returnsuper.clone();}@OverridepublicCallbackgetCallback(intn){MethodInterceptormethodInterceptor;WorldServiceImpl$$EnhancerByCGLIB$$81790592.CGLIB$BIND_CALLBACKS(this);switch(n){case0:{methodInterceptor=this.CGLIB$CALLBACK_0;break;}default:{methodInterceptor=null;}}returnmethodInterceptor;}publicstaticvoidCGLIB$SET_STATIC_CALLBACKS(Callback[]callbackArray){CGLIB$STATIC_CALLBACKS=callbackArray;}publicstaticvoidCGLIB$SET_THREAD_CALLBACKS(Callback[]callbackArray){CGLIB$THREAD_CALLBACKS.set(callbackArray);}}
publicclassWorldServiceBeforeAdviceimplementsMethodBeforeAdvice{@Overridepublicvoidbefore(Methodmethod,Object[]args,Objecttarget)throwsThrowable{System.out.println("BeforeAdvice: do something before the earth explodes");}}
publicObjectgetProxy(){returnProxy.newProxyInstance(getClass().getClassLoader(),advised.getTargetSource().getTargetClass(),this);}@OverridepublicObjectinvoke(Objectproxy,Methodmethod,Object[]args)throwsThrowable{// 获取目标对象Objecttarget=advised.getTargetSource().getTarget();Class<?>targetClass=target.getClass();ObjectretVal=null;// 获取拦截器链List<Object>chain=this.advised.getInterceptorsAndDynamicInterceptionAdvice(method,targetClass);if(chain==null||chain.isEmpty()){returnmethod.invoke(target,args);}else{// 将拦截器统一封装成ReflectiveMethodInvocationMethodInvocationinvocation=newReflectiveMethodInvocation(proxy,target,method,args,targetClass,chain);// Proceed to the joinpoint through the interceptor chain.// 执行拦截器链retVal=invocation.proceed();}returnretVal;}
/** * Apply before-instantiation post-processors, resolving whether there is a * before-instantiation shortcut for the specified bean. * @param beanName the name of the bean * @param mbd the bean definition for the bean * @return the shortcut-determined bean instance, or {@code null} if none */@SuppressWarnings("deprecation")@NullableprotectedObjectresolveBeforeInstantiation(StringbeanName,RootBeanDefinitionmbd){Objectbean=null;if(!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)){// Make sure bean class is actually resolved at this point.if(!mbd.isSynthetic()&&hasInstantiationAwareBeanPostProcessors()){Class<?>targetType=determineTargetType(beanName,mbd);if(targetType!=null){bean=applyBeanPostProcessorsBeforeInstantiation(targetType,beanName);if(bean!=null){bean=applyBeanPostProcessorsAfterInitialization(bean,beanName);}}}mbd.beforeInstantiationResolved=(bean!=null);}returnbean;}
/** * Create a proxy with the configured interceptors if the bean is * identified as one to proxy by the subclass. * @see #getAdvicesAndAdvisorsForBean */@Override@NullablepublicObjectpostProcessAfterInitialization(@NullableObjectbean,StringbeanName){if(bean!=null){ObjectcacheKey=getCacheKey(bean.getClass(),beanName);if(this.earlyBeanReferences.remove(cacheKey)!=bean){returnwrapIfNecessary(bean,beanName,cacheKey);}}returnbean;}