您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringFramework中ProxyFactory的用法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“SpringFramework中ProxyFactory的用法”吧!
Spring版本是5.0.4.release.
ProxyFactory在Springaop中占有舉足輕重的地位,用來間接創(chuàng)建代理,如下List-1所示,我們給ServiceImpl創(chuàng)建代理。
List-1
public interface IService { String hello(); } public class ServiceImpl implements IService { @Override public String hello() { System.out.println("service的hello方法"); return "Hello"; } } import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class BeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("beforeAdvice的before方法"); } } import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; public class AfterAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("AfterAdvice的afterReturning方法"); } } import org.junit.Test; import org.springframework.aop.framework.ProxyFactory; public class AopTest { @Test public void test(){ ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setInterfaces(IService.class); proxyFactory.setTarget(new ServiceImpl()); proxyFactory.addAdvice(new BeforeAdvice()); proxyFactory.addAdvice(new AfterAdvice()); IService proxy = (IService) proxyFactory.getProxy(); String result = proxy.hello(); System.out.println(result); } }
運(yùn)行結(jié)果如下List-2
List-2
beforeAdvice的before方法 service的hello方法 AfterAdvice的afterReturning方法 Hello
proxyFactory.addAdvice(new BeforeAdvice())時(shí),會(huì)將Advice轉(zhuǎn)換為Advisor,最后再調(diào)用addAdvisor方法。
當(dāng)我們調(diào)用ProxyFactory的getProxy時(shí),會(huì)先調(diào)用createAopProxy()->getAopProxyFactory().createAopProxy(this),AopProxyFactory是DefaultAopProxyFactory——在另一篇里面講到。createAopProxy方法里面把this傳入,ProxyFactory實(shí)現(xiàn)了AdvisedSupport,所以在createAopProxy時(shí)將ProxyFactory中設(shè)置的targetSource、advice等傳遞到了DefaultAopProxyFactory,之后傳遞到JdkDynamicAopProxy中。
最后getProxy調(diào)用的是JdkDynamicAopProxy的getProxy方法,如下List-3所示,Proxy.newProxyInstance的方法中,最后的InvocationHandler是this——JdkDynamicAopProxy實(shí)現(xiàn)了InvocationHandler,所以當(dāng)我們調(diào)用目標(biāo)類的方法時(shí),會(huì)調(diào)用JdkDynamicAopProxy的invoke方法。
List-3
public Object getProxy(@Nullable ClassLoader classLoader) { if (logger.isDebugEnabled()) { logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource()); } Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true); findDefinedEqualsAndHashCodeMethods(proxiedInterfaces); return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); }
這里就引出了一個(gè)問題,在JdkDynamicAopProxy的invoke方法中,攔截器鏈?zhǔn)窃趺礃?gòu)造出來了。
感謝各位的閱讀,以上就是“SpringFramework中ProxyFactory的用法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)SpringFramework中ProxyFactory的用法這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。