Spring AOP 源码探索 之 AOP核心类的注册及执行时机分析

文章目录

    • EnableAspectJAutoProxy 开启AOP
      • AspectJAutoProxyRegistrar类分析
    • AOP核心类分析
      • AnnotationAwareAspectAutoProxyCreator
      • AbstractAutoProxyCreator
        • postProcessBeforeInstantiation方法
          • 执行时机
          • 详细执行过程
        • postProcessAfterInitialization方法
          • 执行时机
          • 详细执行过程
  • 相关学习路线
    • JAVA资深架构师成长路线->开源框架解读->Spring框架源码解读

EnableAspectJAutoProxy 开启AOP

开启AOP功能可用注解形式开启:
@EnableAspectJAutoProxy
看下@EnableAspectJAutoProxy注解的作用
在这里插入图片描述
由上图源码可知,EnableAspectJAutoProxy注解导入了AspectJAutoProxyRegist类。

AspectJAutoProxyRegistrar类分析

分析下AspectJAutoProxyRegist类的作用。
AspectJAutoProxyRegist类继承了ImportBeanDefinitionRegistrar。

class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar

会在spring ioc 扫描类定义的过程中调用里面的registerBeanDfinitions方法。
在这里插入图片描述
然后通过AopConfigUtils工具类在IOC容器中注册了AnnotationAwareAspectAutoProxyCreator类
在这里插入图片描述

AOP核心类分析

AnnotationAwareAspectAutoProxyCreator

以下是AnnotationAwareAspectAutoProxyCreator类的继承关系类图
Spring AOP 源码探索 之 AOP核心类的注册及执行时机分析_第1张图片
根据以上类图,分析后得到如下图
Spring AOP 源码探索 之 AOP核心类的注册及执行时机分析_第2张图片
根居上面的类图可知,AnnotationAwareAspectAutoProxyCreator的父类AbstractAutoProxyCreator实现了其中的InstantiationAwareBeanPostProcessor接口方法postProcessBeforeInstantiationpostProcessAfterInitialization

AbstractAutoProxyCreator

postProcessBeforeInstantiation方法

执行时机

postProcessBeforeInstantiation方法会在IOC容器实例化Bean的过程中,创建Bean之前调用。
调用处:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
#resolveBeforeInstantiation
Spring AOP 源码探索 之 AOP核心类的注册及执行时机分析_第3张图片

Spring AOP 源码探索 之 AOP核心类的注册及执行时机分析_第4张图片

详细执行过程

此方法的作用,是将所有的增强方法转化为adivsors,后续文章会详细讲解。
Spring AOP 源码探索 之深入解析实例化切面通知AspectJAdvice 生成 advisors

postProcessAfterInitialization方法

执行时机

postProcessAfterInitialization,则是在Bean创建完,并执行完成初始化方法之后进行调用 。

此方法的作用是,根据切入点判断是否需要生成代理类,如果需要则根据前面的postProcessBeforeInstantiation方法中扫描出的advisors进行代理增强,并返回代理类。

实际调用 :
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean
方法里面的applyBeanPostProcessorsAfterInitialization方法

initializeBean源码

	protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
		if (System.getSecurityManager() != null) {
			AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
				invokeAwareMethods(beanName, bean);
				return null;
			}, getAccessControlContext());
		}
		else {
			//调用XXXAware方法
			//调用实现了BeanNameAware的Bean的setBeanName方法,用来设置名称
			//调用实现了BeanClassLoaderAware的Bean的setBeanClassLoader方法,用来设置Bean的加载器
			//调用实现了BeanFactoryAware的Bean的setBeanFactory方法,用来设置使用的Bean工厂是哪个
			invokeAwareMethods(beanName, bean);
		}

		Object wrappedBean = bean;
		if (mbd == null || !mbd.isSynthetic()) {
			//调用 BeanPostProcessors 后置处理器的 postProcessBeforeInitialization 方法
			wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
		}

		try {
			//调用 Bean 的 init 初始化方法
			invokeInitMethods(beanName, wrappedBean, mbd);
		}
		catch (Throwable ex) {
			throw new BeanCreationException(
					(mbd != null ? mbd.getResourceDescription() : null),
					beanName, "Invocation of init method failed", ex);
		}
		if (mbd == null || !mbd.isSynthetic()) {
			//调用 BeanPostProcessors 后置处理器的 postProcessAfterInitialization 方法
			wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
		}

		return wrappedBean;
	}

applyBeanPostProcessorsAfterInitialization方法图示
Spring AOP 源码探索 之 AOP核心类的注册及执行时机分析_第5张图片
applyBeanPostProcessorsAfterInitialization方法调用了所有processor的postProcessAfterInitialization方法。

详细执行过程

后续文章同样会详细讲解代理类创建和增强方法调用过程。
Spring AOP 源码探索 之深入解析实例化Bean过程创建代理类$Proxy

相关学习路线

JAVA资深架构师成长路线->开源框架解读->Spring框架源码解读

你可能感兴趣的:(AOP,Spring源码,开源框架)