SpringFramework中ProxyFactory的用法
这篇文章主要讲解了“SpringFramework中ProxyFactory的用法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringFramework中ProxyFactory的用法”吧!
成都创新互联-专业网站定制、快速模板网站建设、高性价比鼎城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式鼎城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖鼎城地区。费用合理售后完善,十多年实体公司更值得信赖。
Spring版本是5.0.4.release.
ProxyFactory在Springaop中占有举足轻重的地位,用来间接创建代理,如下List-1所示,我们给ServiceImpl创建代理。
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); } }
运行结果如下List-2
List-2
beforeAdvice的before方法 service的hello方法 AfterAdvice的afterReturning方法 Hello
proxyFactory.addAdvice(new BeforeAdvice())时,会将Advice转换为Advisor,最后再调用addAdvisor方法。
当我们调用ProxyFactory的getProxy时,会先调用createAopProxy()->getAopProxyFactory().createAopProxy(this),AopProxyFactory是DefaultAopProxyFactory——在另一篇里面讲到。createAopProxy方法里面把this传入,ProxyFactory实现了AdvisedSupport,所以在createAopProxy时将ProxyFactory中设置的targetSource、advice等传递到了DefaultAopProxyFactory,之后传递到JdkDynamicAopProxy中。
最后getProxy调用的是JdkDynamicAopProxy的getProxy方法,如下List-3所示,Proxy.newProxyInstance的方法中,最后的InvocationHandler是this——JdkDynamicAopProxy实现了InvocationHandler,所以当我们调用目标类的方法时,会调用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); }
这里就引出了一个问题,在JdkDynamicAopProxy的invoke方法中,拦截器链是怎么构造出来了。
感谢各位的阅读,以上就是“SpringFramework中ProxyFactory的用法”的内容了,经过本文的学习后,相信大家对SpringFramework中ProxyFactory的用法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
分享题目:SpringFramework中ProxyFactory的用法
URL分享:http://hbruida.cn/article/pjjsgc.html