看proxyFactory.addAdvice主要干了什么?
看下繼承關(guān)系:
?將advisor加入advisors
?
看下如何生成代理對象
org.springframework.aop.framework.DefaultAopProxyFactory#createAopProxy?
org.springframework.aop.framework.DefaultAopProxyFactory#hasNoUserSuppliedProxyInterfaces
?判斷是否有接口,沒有接口直接返回true
?如果是接口或者代理類直接返回jdk動(dòng)態(tài)代理
否則返回cglib代理?
org.springframework.aop.framework.CglibAopProxy#getProxy(java.lang.ClassLoader)
看看如何創(chuàng)建代理對象。
?org.springframework.aop.framework.ObjenesisCglibAopProxy#createProxyClassAndInstance
真正的創(chuàng)建代理對象的邏輯:
org.springframework.cglib.proxy.Enhancer#createHelper
org.springframework.cglib.core.AbstractClassGenerator#create
看下代理對象執(zhí)行過程:
?org.springframework.aop.framework.CglibAopProxy.DynamicAdvisedInterceptor#intercept
org.springframework.aop.framework.ReflectiveMethodInvocation#proceed?
責(zé)任鏈模式,第一次肯定不會相等。
?這里會調(diào)用我們手寫的方法,然后執(zhí)行processor
?
?再次進(jìn)來回去執(zhí)行目標(biāo)方法
?
?這樣就反射到目標(biāo)方法了,整個(gè)流程結(jié)束。
1.總結(jié):核心邏輯,重要代碼如下:
org.springframework.aop.framework.AdvisedSupport#addAdvice(int, org.aopalliance.aop.Advice)
裝飾者模式:advisor包括advice
?2.代理對象使用哪種模式,jdk or cglib
org.springframework.aop.framework.ProxyFactory#getProxy()
3.代理對象的產(chǎn)生
org.springframework.aop.framework.CglibAopProxy#getProxy()
org.springframework.aop.framework.CglibAopProxy#createProxyClassAndInstance
org.springframework.cglib.proxy.Enhancer#createHelper
org.springframework.cglib.core.AbstractClassGenerator#create
4.代理對象執(zhí)行業(yè)務(wù)邏輯:
org.springframework.aop.framework.CglibAopProxy.DynamicAdvisedInterceptor#intercept
獲取所有執(zhí)行鏈
org.springframework.aop.framework.AdvisedSupport#getInterceptorsAndDynamicInterceptionAdvice
?獲取攔截器
org.springframework.aop.framework.adapter.DefaultAdvisorAdapterRegistry#getInterceptors
?這里有個(gè)攔截器 c$1
org.springframework.aop.framework.ReflectiveMethodInvocation#ReflectiveMethodInvocation
構(gòu)造對象,interceptorsAndDynamicMethodMatchers這個(gè)有值了,后續(xù)會使用到
?org.springframework.aop.framework.ReflectiveMethodInvocation#proceed
第一次肯定不會去執(zhí)行代理邏輯:
?下面回去執(zhí)行我們寫的代理邏輯,然后執(zhí)行 invocation.proceed();
文章來源:http://www.zghlxwxcb.cn/news/detail-406106.html
?再次進(jìn)來就會去反射執(zhí)行目標(biāo)方法,目標(biāo)方法執(zhí)行完了,整個(gè)流程結(jié)束文章來源地址http://www.zghlxwxcb.cn/news/detail-406106.html
到了這里,關(guān)于Spring封裝的動(dòng)態(tài)代理的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!