前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringFramework之DefaultAopProxyFactory

SpringFramework之DefaultAopProxyFactory

作者头像
克虏伯
发布2019-07-15 14:38:03
6800
发布2019-07-15 14:38:03
举报

    Spring版本是5.0.4.release.

    Springaop中的DefaultAopProxyFactory,先上一张图,如下图1

                                                                                            图1

    Springaop中使用DefaultAopProxyFactory来创建代理,它决定了何时使用JDK还是Cglib代理,实现如下List-1所示:

List-1

代码语言:javascript
复制
public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {

	@Override
	public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
		if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
			Class<?> targetClass = config.getTargetClass();
			if (targetClass == null) {
				throw new AopConfigException("TargetSource cannot determine target class: " +
						"Either an interface or a target is required for proxy creation.");
			}
			if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
				return new JdkDynamicAopProxy(config);
			}
			return new ObjenesisCglibAopProxy(config);
		}
		else {
			return new JdkDynamicAopProxy(config);
		}
	}

	/**
	 * Determine whether the supplied {@link AdvisedSupport} has only the
	 * {@link org.springframework.aop.SpringProxy} interface specified
	 * (or no proxy interfaces specified at all).
	 */
	private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {
		Class<?>[] ifcs = config.getProxiedInterfaces();
		return (ifcs.length == 0 || (ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0])));
	}
}

    List-1中,目标对象拥有接口实现且没设置proxyTargetClass=true,则使用JDK代理,此外如果目标对象是个接口或者是代理类,则使用JDK代理,否则使用Cglib,有些人说设置了ProxyTargetClass=true会使用JDK代理,正常情况下是正确的,不过从源码来看不一定——即使设置了ProxyTargetClass=true,如果目标对象类是个接口或者是代理类,则使用使用的是JDK代理。

Reference

  1. https://github.com/spring-projects/spring-framework/tree/5.0.x

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reference
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档