SpringFramework之DefaultAopProxyFactory

    Spring版本是5.0.4.release.

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

         

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

Reference

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

 

转载于:https://my.oschina.net/u/2518341/blog/3073752

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值