自我分析-Spring AOP

本文深入探讨Spring AOP的实现机制,从自动装箱BeanFactory出发,剖析createBean入口,详细解读对象实例化前处理、代理对象创建及JDK与CGLIB动态代理过程,揭示Spring AOP如何通过动态代理实现方法增强。
摘要由CSDN通过智能技术生成

Spring AOP直白的来说就是使用了JDK的动态代理和CGLIB对Bean的代理,从而实现方法增强的效果。

注意:

1.分析框架代码时,要常使用类继承、调用关系等快捷键,可以更高效的学习,快捷键可以设置成你习惯的按键;

2.本文重在怎么自我分析框架代码,所以对其中解析需自己实际跟踪代码实践方可;

3.spring源代码版本 spring-framework-3.2.1.RELEASE。


预览

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory,自动装箱BeanFactory。

org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator,代理对象创建类。

org.springframework.aop.framework.JdkDynamicAopProxy,JDK动态代理类。

org.springframework.aop.framework.CglibAopProxy,CGLIB动态代理类。



入口-createBean

前面我们分析Spring IOC时,了解了IOC的资源定位、加载、注册等步骤,但是对于AOP的实现还未分析。

还记得createBean嘛,这里就是实际创建Bean的入口,在getBean中调用了。


org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory

public <T> T createBean(Class<T> beanClass) throws BeansException {
		// Use prototype bean definition, to avoid registering bean as dependent bean.
		RootBeanDefinition bd = new RootBeanDefinition(beanClass);
		bd.setScope(SCOPE_PROTOTYPE);
		bd.allowCaching = false;
		return (T) createBean(beanClass.getName(), bd, null);
	}
protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
			throws BeanCreationException {

		if (logger.isDebugEnabled()) {
			logger.debug("Creating instance of bean '" + beanName + "'");
		}
		// Make sure bean class is actually resolved at this point.
		resolveBeanClass(mbd, beanName);

		// Prepare method overrides.
		try {
			mbd.prepareMethodOverrides();
		}
		catch (BeanDefinitionValidationException ex) {
			throw new BeanDefinitionStoreException(mbd.getResourceDescription(),
					beanName, "Validation of method overrides failed", ex);
		}

		try {
			// 产生 AOP 代理对象的入口
			Object bean = resolveBeforeInstantiation(beanName, mbd);
			if (bean != null) {
				return bean;
			}
		}
		catch (Throwable ex) {
			throw new BeanCreationException(mbd.getResourceDescription(), beanName,
					"BeanPostProcessor before instantiation of bean failed", ex);
		}

		Object beanInstance = doCreateBean(beanName, mbd, args);
		if (logger.isDebugEnabled()) {
			logger.debug("Finished creating instance of bean '" + beanName + "'");
		}
		return beanInstance;
	}


对象实例化前处理

在对象实例化前做相应的逻辑处理,即是否需创建代理对象

protected Object applyBeanPostProcessorsBeforeInstantiation(Class beanClass, String beanName)
			throws BeansException {

		for (BeanPostProcessor bp : getBeanPostProcessors()) {
			if (bp instanceof InstantiationAwareBeanPostProcessor) {
				InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
				// 在对象实例化前的处理操作
				Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
				if (result != null) {
					return result;
				}
			}
		}
		return null;
	}

在实例化前会判断是否存在AOP的代理,而此方法的实现在org.springframework.aop.framework.autoproxy. AbstractAutoProxyCreator。

<strong>	</strong>public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		Object cacheKey = getCacheKey(beanClass, beanName);

		if (beanName == null || !this.targetSourcedBeans.containsKey(beanName)) {
			if (this.advisedBeans.containsKey(cacheKey)) {
				return null;
			}
			if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
				this.advisedBeans.put(cacheKey, Boolean.FALSE);
				return null;
			}
		}

		// Create proxy here if we have a custom TargetSource.
		// Suppresses unnecessary default instantiation of the target bean:
		// The TargetSource will handle target instances in a custom fashion.
		if (beanName != null) {
			TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
			if (targetSource != null) {
				this.targetSourcedBeans.put(beanName, Boolean.TRUE);
				// 获取通知点即需增强效果的接口,如想了解可查看其子类AbstractAdvisorAutoProxyCreator
				Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
				// 根据类名、需增强接口等信息创建代理对象
				Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
				this.proxyTypes.put(cacheKey, proxy.getClass());
				return proxy;
			}
		}

		return null;
	}


生产代理对象的准备工作

接下来将看到一系列的准备工作:代理工厂、目标对象的接口、目标对象的通知者等。

<span style="white-space:pre">	</span>protected Object createProxy(
			Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) {

		ProxyFactory proxyFactory = new ProxyFactory();
		// Copy our properties (proxyTargetClass etc) inherited from ProxyConfig.
		proxyFactory.copyFrom(this);

		if (!shouldProxyTargetClass(beanClass, beanName)) {
			// 获取所有目标对象的接口
			Class<?>[] targetInterfaces = ClassUtils.getAllInterfacesForClass(beanClass, this.proxyClassLoader);
			for (Class<?> targetInterface : targetInterfaces) {
				proxyFactory.addInterface(targetInterface);
			}
		}

		// 通过通知点即类名创建通知者(切入点及增强效果的集合)
		Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
		for (Advisor advisor : advisors) {
			proxyFactory.addAdvisor(advisor);
		}

		proxyFactory.setTargetSource(targetSource);
		customizeProxyFactory(proxyFactory);

		proxyFactory.setFrozen(this.freezeProxy);
		if (advisorsPreFiltered()) {
			proxyFactory.setPreFiltered(true);
		}

		// 通过工厂和proxyClassLoader来获取代理对象
		return proxyFactory.getProxy(this.proxyClassLoader);
	}


创建JDK动态代理 OR CGLIB

<span style="white-space:pre">	</span>public Object getProxy(ClassLoader classLoader) {
		return createAopProxy().getProxy(classLoader); //使用
	}

此处的createAopProxy(),是使用org.springframework.aop.framework. DefaultAopProxyFactory中的createAopProxy来

创建JdkDynamicAopProxy还是CglibProxyFactory。

其中JDK的动态代理只能对接口型的目标对象进行代理,而CGLIB没有此限制,代码中我们可以明确的看到。

<span style="white-space:pre">	</span>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()) { // 目标对象若是接口,即生产JDK的动态代理
				return new JdkDynamicAopProxy(config);
			}
			return CglibProxyFactory.createCglibProxy(config); // 非接口型目标对象,即使用CGLIB来实现代理。
		}
		else {
			return new JdkDynamicAopProxy(config);
		}
	}


接下来就是的getProxy生产代理对象就要看JdkDynamicAopProxy和CglibProxyFactory两个类了,若对两者的动态代理不熟悉,先实现了解下,

再接着分析。

JDK动态代理:http://blog.csdn.net/xiaohai0504/article/details/6884925

CGLIB动态代理:http://blog.csdn.net/xiaohai0504/article/details/6832990




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值