Spring源码解析(四)-Spring使用cglib创建代理对象

Spring源码解析

办公设备租赁,深圳惠源.


一、ProxyFactory

Spring通过ProxyFactory创建代理对象,可以看Spring源码解析(三)

// 创建代理对象
public Object getProxy() {
		return createAopProxy().getProxy();
	}
// 创建代理对象
protected final synchronized AopProxy createAopProxy() {
		if (!this.active) {
			activate();
		}
		return getAopProxyFactory().createAopProxy(this);
	}
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
// config.isProxyTargetClass() 是否设置了proxyTargetClass为true 
// hasNoUserSuppliedProxyInterfaces 是否没有实现任何接口
		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.");
			}
			// 代理类本身是个接口需要用jdk代理
			if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
				return new JdkDynamicAopProxy(config);
			}
			// 其余使用cglib
			return new ObjenesisCglibAopProxy(config);
		}
		else {
			return new JdkDynamicAopProxy(config);
		}

从以上可以看出一般情况下使用jdk代理,除非设置了proxyTargetClass为true或者没有实现接口,所有我们主要看下proxyTargetClass在那个位置设置

在这里插入图片描述

获取注解属性proxyTargetClass为true的时候执行AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry),而proxyTargetClass是@EnableTransactionManagement的属性

在这里插入图片描述

AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry)

public static final String AUTO_PROXY_CREATOR_BEAN_NAME =
			"org.springframework.aop.config.internalAutoProxyCreator";

// 强制使用cglib代理
public static void forceAutoProxyCreatorToUseClassProxying(BeanDefinitionRegistry registry) {
// 判断是否有传教代理的Creator
		if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
			BeanDefinition definition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
			// 为代理设置proxyTargetClass 设置为true
		definition.getPropertyValues().add("proxyTargetClass", Boolean.TRUE);
		}
	}

从forceAutoProxyCreatorToUseClassProxying点击引用,看到AopAutoConfig也调用了该方法

在这里插入图片描述

ClassProxyingConfiguration

// 设置spring.aop.proxy-target-class = true的时候该类生效
@Configuration(proxyBeanMethods = false)
	@ConditionalOnMissingClass("org.aspectj.weaver.Advice")
	@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",
			matchIfMissing = true)
	static class ClassProxyingConfiguration {

		ClassProxyingConfiguration(BeanFactory beanFactory) {
			if (beanFactory instanceof BeanDefinitionRegistry) {
				BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
// 注册AutoProxyCreator
				AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
// 强制使用cglib
				AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
			}
		}

	}

总结

当设置@EnableTransactionManagement的proxyTargetClass=true,或者spring.aop.proxy-target-class为true的时候使用cglib代理 [办公设备租赁,深圳惠源](https://www.huiyuan136.com/).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值