SpringFramework中DefaultAopProxyFactory的作用是什么

发布时间:2021-06-22 14:37:20 作者:Leah
来源:亿速云 阅读:120

本篇文章为大家展示了SpringFramework中DefaultAopProxyFactory的作用是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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

         SpringFramework中DefaultAopProxyFactory的作用是什么

                                                                                            图1

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

    List-1

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代理。

上述内容就是SpringFramework中DefaultAopProxyFactory的作用是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. javascript中this的作用是什么
  2. python中%的作用是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springframework

上一篇:PHP如何实现Redis多库选择功能单

下一篇:php如何实现自定义中奖项数和概率抽奖

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》