欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【Spring AOP源码】基于JDK动态代理和Cglib创建代理对象的原理分析

程序员文章站 2022-03-02 23:38:26
本文直接进入正题,从AOP代理对象创建器说起。AOP代理对象创建器:AspectJAwareAdvisorAutoProxyCreator其中,InstantiationAwareBeanPostProcessor接口是BeanPostProcessor接口的子接口,主要作用:为特殊定制目标bean的实例化过程,如给目标对象创建代理对象。BeanPostProcessor接口,即bean后置处理器,作用是在Bean对象在实例化和依赖注入完毕后,在显示调用初始化方法的前后添加我们自己的逻辑....

Spring 版本:version=5.1.0.BUILD-SNAPSHOT

前置知识

BeanPostProcessor简介

BeanPostProcessor接口,即bean后置处理器,作用是当Bean对象在实例化和依赖注入完毕后,显示调用初始化方法的前后,添加我们自己的逻辑。注意是Bean实例化完毕后及依赖注入完成后触发的。

简单的讲就是在Bean初始化的前后做一些事情,里面有两个方法,一个是postProcessBeforeInitialization,实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务。另一个是postProcessAfterInitialization,实例化、依赖注入、初始化完毕时执行。

public interface BeanPostProcessor {

    // bean的自定义初始化方法之前执行
	@Nullable
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

    //bean的自定义初始化方法之后执行
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

}

InstantiationAwareBeanPostProcessor简介

InstantiationAwareBeanPostProcessor接口是BeanPostProcessor接口的子接口,Instantiation翻译是实例化的意思,也就是说这个接口是在Bean实例化的前后做一些事情。主要作用:为特殊定制目标bean的实例化过程,如给目标对象创建代理对象。所以该接口的实现类需要实现5个方法。

public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {

    // 实例化前
	@Nullable
	default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		return null;
	}

    // 实例化之后,属性填充之前执行
	default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		return true;
	}

    // 属性填充之前,修改Bean中属性的内容
    // 在这里可以修改即将要填充属性的值,自动注入是在这里进行的。
	@Nullable
	default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
			throws BeansException {

		return null;
	}

    // spring5.1之后,该方法被抛弃,使用postProcessProperties代替
    @Deprecated
	@Nullable
	default PropertyValues postProcessPropertyValues(
			PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {

		return pvs;
	}

}

AOP代理对象创建器:AspectJAwareAdvisorAutoProxyCreator

【Spring AOP源码】基于JDK动态代理和Cglib创建代理对象的原理分析

从上图的AspectJAwareAdvisorAutoProxyCreator的类继承体系结构图中,我们发现,它实现了InstantiationAwareBeanPostProcessor接口,具体为在抽象父类AbstractAutoProxyCreator:同时重写了postProcessBeforeInstantiation方法的实现(它实现了上面的5个方法)。

因此,AspectJAwareAdvisorAutoProxyCreator 也是一个bean后置处理器,它的作用是在bean对象实例化的前后可以进行一些操作。现在需要思考的是对于AspectJAwareAdvisorAutoProxyCreator这个对象是什么时候生成的呢?其实它在registerBeanPostProcessors注册bean后置处理器的时候实例化的。下面先进入refesh方法:

因此,我们先进入到AbstractApplicationContext#refresh方法

	@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			// 初始化属性配置文件、校验必要属性以及监听器
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			// 告诉子类刷新内部bean工厂,给beanFactory设置序列化id
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			// 向beanFactory中注册了两个BeanPostProcessor,以及三个与环境相关的bean
			// 这两个后置处理器为ApplicationContextAwareProcessor和ApplicationListenerDetector
			// 前一个后置处理是为实现了ApplicationContextAware接口的类,回调setApplicationContext()方法,
			// 后一个处理器时用来检测ApplicationListener类的,当某个Bean实现了ApplicationListener接口的bean被创建好后,会被加入到监听器列表中
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				// 空方法,由子类实现
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				// 执行所有的BeanFactoryPostProcessor,包括自定义的,以及spring内置的。
				// 默认情况下,容器中只有一个BeanFactoryPostProcessor,即:Spring内置的,ConfigurationClassPostProcessor(这个类很重要)
				// 会先执行实现了BeanDefinitionRegistryPostProcessor接口的类,然后执行BeanFactoryPostProcessor的类
				// ConfigurationClassPostProcessor类的postProcessorBeanFactory()方法进行了@Configuration类的解析,@ComponentScan的扫描,以及@Import注解的处理
				// 经过这一步以后,会将所有交由spring管理的bean所对应的BeanDefinition放入到beanFactory的beanDefinitionMap中
				// 同时ConfigurationClassPostProcessor类的postProcessorBeanFactory()方法执行完后,向容器中添加了一个后置处理器————ImportAwareBeanPostProcessor
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				// 注册所有的BeanPostProcessor,因为在方法里面调用了getBean()方法,所以在这一步,实际上已经将所有的BeanPostProcessor实例化了
				// 为什么要在这一步就将BeanPostProcessor实例化呢?因为后面要实例化bean,而BeanPostProcessor是用来干预bean的创建过程的,
				// 所以必须在bean实例化之前就实例化所有的BeanPostProcessor(包括开发人员自己定义的)
				// 最后再重新注册了ApplicationListenerDetector,这样做的目的是为了将ApplicationListenerDetector放入到后置处理器的最末端
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				// 初始化MessageSource,用来做消息国际化。在一般项目中不会用到消息国际化
				initMessageSource();

				// Initialize event multicaster for this context.
				// 初始化事件广播器,如果容器中存在了名字为applicationEventMulticaster的广播器,则使用该广播器
				// 如果没有,则初始化一个SimpleApplicationEventMulticaster
				// 事件广播器的用途是,发布事件,并且为所发布的时间找到对应的事件监听器。
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				// 执行其他的初始化操作,例如和SpringMVC整合时,需要初始化一些其他的bean,但是对于纯spring工程来说,onFresh方法是一个空方法
				onRefresh();

				// Check for listener beans and register them.
				// 这一步会将自定义的listener的bean名称放入到事件广播器中
				// 同时还会将早期的ApplicationEvent发布(对于单独的spring工程来说,在此时不会有任何ApplicationEvent发布,
				// 但是和springMVC整合时,springMVC会执行onRefresh()方法,在这里会发布事件)
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				// 实例化剩余的非懒加载的单例bean(注意:剩余、非懒加载、单例)
				// 为什么说是剩余呢?如果开发人员自定义了BeanPostProcessor,而BeanPostProcessor在前面已经实例化了,所以在这里不会再实例化,因此这里使用剩余一词
				// 注:该方法十分重要,它完成了所有非懒加载的单例Bean的实例化和初始化,属性的填充以及解决了循环依赖等问题
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				// 结束refresh,主要干了一件事,就是发布一个事件ContextRefreshEvent,通知大家spring容器refresh初始化结束了。
				finishRefresh();
			} catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				// 出异常后销毁bean
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			} finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				// 在bean的实例化过程中,会缓存很多信息,例如bean的注解信息,
				// 但是当单例bean实例化完成后,这些缓存信息已经不会再使用了,所以可以释放这些内存资源了
				resetCommonCaches();
			}
		}
	}

其中,registerBeanPostProcessors()就是进行所有的bean后置处理器的注册实例化

	// 注册实例化所有的bean后置处理器,放入beanFactory属性中
	public static void registerBeanPostProcessors(
			ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

		//1、获取所有的bean后置处理器的name,再根据name去实例化对应的BeanPostProcessor
		String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

		// Register BeanPostProcessorChecker that logs an info message when
		// a bean is created during BeanPostProcessor instantiation, i.e. when
		// a bean is not eligible for getting processed by all BeanPostProcessors.
		int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
		beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

		//2、对这些后置处理器进行排序分类存储 list
		// Separate between BeanPostProcessors that implement PriorityOrdered,
		// Ordered, and the rest.
		List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
		List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
		List<String> orderedPostProcessorNames = new ArrayList<>();
		List<String> nonOrderedPostProcessorNames = new ArrayList<>();
		for (String ppName : postProcessorNames) {
			if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
				priorityOrderedPostProcessors.add(pp);
				if (pp instanceof MergedBeanDefinitionPostProcessor) {
					internalPostProcessors.add(pp);
				}
			} else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
				orderedPostProcessorNames.add(ppName);
			} else {
				nonOrderedPostProcessorNames.add(ppName);
			}
		}

		//3、以下是根据排序的优先级将所有的BeanPostProcessors后置处理器注册实例化,并放入AbstractBeanFactory的属性中
		// -->即 List<BeanPostProcessor> beanPostProcessors

		// (1)首先,注册实现了PriorityOrdered接口的BeanPostProcessors
		// First, register the BeanPostProcessors that implement PriorityOrdered.
		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);//排序
		registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

		// (2)然后,注册实现了Ordered接口的BeanPostProcessors
		// Next, register the BeanPostProcessors that implement Ordered.
		List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
		for (String ppName : orderedPostProcessorNames) {

			// AspectJAwareAdvisorAutoProxyCreator的实例化就是在这里进行的,因为它也属于BeanPostProcessor的子类
			// 那么对于其它bean实例化的时候它就可以起到后置处理器的作用--> AOP生成代理对象
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);

			orderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		sortPostProcessors(orderedPostProcessors, beanFactory);//排序
		registerBeanPostProcessors(beanFactory, orderedPostProcessors);

		// (3)再注册所有普通的BeanPostProcessor(未实现PriorityOrdered/Ordered接口的bean后置处理器)
		// Now, register all regular BeanPostProcessors.
		List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
		for (String ppName : nonOrderedPostProcessorNames) {
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
			nonOrderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

		// (4)最后,注册所有的 internalPostProcessors
		// Finally, re-register all internal BeanPostProcessors.
		sortPostProcessors(internalPostProcessors, beanFactory);
		registerBeanPostProcessors(beanFactory, internalPostProcessors);

		// Re-register post-processor for detecting inner beans as ApplicationListeners,
		// moving it to the end of the processor chain (for picking up proxies etc).
		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
	}

其中,AspectJAwareAdvisorAutoProxyCreator实现了Ordered接口,所以它是在上面的第(2)步进行实例化的,那么对于其它bean实例化的时候它就可以起到后置处理器的作用了-->AOP,即给目标对象产生代理对象。

现在,我们已经知道AOP代理对象创建器(AspectJAwareAdvisorAutoProxyCreator)在哪里创建了,接下来就是寻找该bean后置处理器在什么时候给目标对象生成代理对象的。

进入AbstractApplicationContext#refresh方法中的 finishBeanFactoryInitialization(beanFactory),看过Spring Bean生命周期源码的,应该都知道,该方法十分重要,它完成了所有剩余的、非懒加载的、单例的bean实例化和初始化过程。为什么说是剩余呢?如果开发人员自定义了BeanPostProcessor,而BeanPostProcessor在上面registerBeanPostProcessors()已经实例化了,所以在这里不会再实例化,因此这里使用剩余一词。

	protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
		// Initialize conversion service for this context.
		if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
				beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
			beanFactory.setConversionService(
					beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
		}

		// Register a default embedded value resolver if no bean post-processor
		// (such as a PropertyPlaceholderConfigurer bean) registered any before:
		// at this point, primarily for resolution in annotation attribute values.
		if (!beanFactory.hasEmbeddedValueResolver()) {
			beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
		}

		// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
		String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
		for (String weaverAwareName : weaverAwareNames) {
			getBean(weaverAwareName);
		}

		// Stop using the temporary ClassLoader for type matching.
		beanFactory.setTempClassLoader(null);

		// Allow for caching all bean definition metadata, not expecting further changes.
		beanFactory.freezeConfiguration();

		// Instantiate all remaining (non-lazy-init) singletons.
        // 主要看这一个方法
		beanFactory.preInstantiateSingletons();
	}

我们进入bean工厂AbstractBeanFactory的默认实现子类 DefaultListableBeanFactory#preInstantiateSingletons()方法,从原注释也可以知道,它才是实现所有剩余的单例bean实例化的方法:

	@Override
	public void preInstantiateSingletons() throws BeansException {
		if (this.logger.isDebugEnabled()) {
			this.logger.debug("Pre-instantiating singletons in " + this);
		}

		// Iterate over a copy to allow for init methods which in turn register new bean definitions.
		// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
		//
		List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

		// Trigger initialization of all non-lazy singleton beans...
		// 触发所有非懒加载的单例bean的初始化...
		for (String beanName : beanNames) {
			RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
			// 非抽象类、单例、非懒加载
			if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {

				// 属于FactoryBean子类的bean
				if (isFactoryBean(beanName)) {
					Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
					if (bean instanceof FactoryBean) {
						final FactoryBean<?> factory = (FactoryBean<?>) bean;
						boolean isEagerInit;
						if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
							isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
											((SmartFactoryBean<?>) factory)::isEagerInit,
									getAccessControlContext());
						} else {
							isEagerInit = (factory instanceof SmartFactoryBean &&
									((SmartFactoryBean<?>) factory).isEagerInit());
						}
						if (isEagerInit) {
							getBean(beanName);
						}
					}
				} else {
					// 普通bean(非FactoryBean子类)
					getBean(beanName);
				}
			}
		}

		// Trigger post-initialization callback for all applicable beans...
		for (String beanName : beanNames) {
			Object singletonInstance = getSingleton(beanName);
			if (singletonInstance instanceof SmartInitializingSingleton) {
				final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
				if (System.getSecurityManager() != null) {
					AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
						smartSingleton.afterSingletonsInstantiated();
						return null;
					}, getAccessControlContext());
				} else {
					smartSingleton.afterSingletonsInstantiated();
				}
			}
		}
	}

从上面源码,可以看到,这里bean的实例化,主要分为两种:

  • FactoryBean子类的实例化
  • 普通bean(非FactoryBean子类)的实例化

至于什么是FactoryBean?它可以让我们自定义Bean的创建过程,主要是通过实现FactoryBean接口的getObject()方法,具体可以看之前单独写的文章《【Spring源码:FactoryBean一】终于弄懂FactoryBean是如何自定义bean的创建过程了》。

最后,都会调用getBean(beanName)方法,我们可以继续追踪该方法源码,发现最终进入AbstractBeanFactory#doGetBean()方法:

	protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
							  @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {

		// beanName格式化转换,如别名、&factoryBean
		final String beanName = transformedBeanName(name);
		Object bean;

		// Eagerly check singleton cache for manually registered singletons.
		// 1、先从单例缓存池中获取
		Object sharedInstance = getSingleton(beanName);
		if (sharedInstance != null && args == null) {
			if (logger.isDebugEnabled()) {
				if (isSingletonCurrentlyInCreation(beanName)) {
					logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
							"' that is not fully initialized yet - a consequence of a circular reference");
				} else {
					logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
				}
			}

			// 这里为啥不直接返回该sharedInstance?? ---> FactoryBean作用:getObject()自定义bean的创建过程.
			bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);

		} else {// 2、单例缓存池获取不到

            //省略部分代码。。。。

            // Create bean instance.
				// 根据bean的作用域范围scope,创建bean实例
				if (mbd.isSingleton()) {// (1)bean作用域:单例
					sharedInstance = getSingleton(beanName, () -> {
						try {
                            // 重点
							return createBean(beanName, mbd, args);
						} 
					});
					bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);

				} else if (mbd.isPrototype()) {// (2)bean作用域:原型
					// It's a prototype -> create a new instance.
					Object prototypeInstance = null;
					try {
						beforePrototypeCreation(beanName);
						prototypeInstance = createBean(beanName, mbd, args);
					} finally {
						afterPrototypeCreation(beanName);
					}
					bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);

				} else {// (3)非单例、非原型
                    。。。。
				}
        。。。。

其中,该方法比较长,这里只截取部分核心代码,主要分两部分功能:

  1. 通过从单例缓存池中获取bean,能获取到(不为空),说明已创建过,则继续调用getObjectForBeanInstance()方法;
  2. 后半部分else是单例池获取不到时,说明是首次创建,需要调用createBean()方法,创建好之后,同样会调用到getObjectForBeanInstance()方法。

【注意】这里为啥获取到bean之后不直接返回,还要调用getObjectForBeanInstance()方法?

因为getObjectForBeanInstance()方法中还要进行判断该bean是FactoryBean子类,还是普通bean,如果是FactoryBean子类,并且name含有前缀&,则返回FactoryBean子类对象本身, 否则,返回FactoryBean子类的getObject()方法中自定义的对象,具体可看下面源码解释(这里也涉及到FactoryBean的知识):

小插曲:AbstractBeanFactory#getObjectForBeanInstance()

	protected Object getObjectForBeanInstance(
			Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {

		// Don't let calling code try to dereference the factory if the bean isn't a factory.
		// 1、如果这是一个FactoryBean子类,并且name含有前缀&,说明需要获取的是FactoryBean子类本身,直接返回即可
		if (BeanFactoryUtils.isFactoryDereference(name)) {
			if (beanInstance instanceof NullBean) {
				return beanInstance;
			}
			if (!(beanInstance instanceof FactoryBean)) {
				throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());
			}
		}

		// Now we have the bean instance, which may be a normal bean or a FactoryBean.
		// If it's a FactoryBean, we use it to create a bean instance, unless the
		// caller actually wants a reference to the factory.
		// 2、如果这是一个普通bean,则直接返回该bean
		if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
			return beanInstance;
		}

		// 3、能执行到这一步,说明它是一个FactoryBean子类,并且name不含&前缀
		// 注意:如果是FactoryBean子类,name含有前缀&,则返回FactoryBean子类对象本身,
		// 否则,返回FactoryBean子类的getObject()方法中自定义的对象
		Object object = null;
		if (mbd == null) {
			object = getCachedObjectForFactoryBean(beanName);//先从缓存中获取
		}
		if (object == null) {
			// Return bean instance from factory.
			FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
			// Caches object obtained from FactoryBean if it is a singleton.

			if (mbd == null && containsBeanDefinition(beanName)) {
				mbd = getMergedLocalBeanDefinition(beanName);
			}
			boolean synthetic = (mbd != null && mbd.isSynthetic());
            // 从FactoryBean中获取对象
			object = getObjectFromFactoryBean(factory, beanName, !synthetic);
		}
		return object;
	}

其中,getObjectFromFactoryBean()方法,我们从名字就可以大概知道它是从FactoryBean中获取对象,由于篇幅原因,这里不详细分析,具体可看《【Spring源码:FactoryBean二】终于弄懂FactoryBean是如何自定义bean的创建过程了》

【重点】

下面,我们继续回到主流程,createBean()方法,它属于AbstractBeanFactory中定义的方法,在AbstractAutowireCapableBeanFactory抽象子类中有默认实现,如下:

	@Override
	protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
			throws BeanCreationException {

		RootBeanDefinition mbdToUse = mbd;

		// 对给定的bean进行类加载
		// Make sure bean class is actually resolved at this point, and
		// clone the bean definition in case of a dynamically resolved Class
		// which cannot be stored in the shared merged bean definition.
		Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
		if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
			mbdToUse = new RootBeanDefinition(mbd);
			mbdToUse.setBeanClass(resolvedClass);
		}

		// Prepare method overrides.
		try {
			mbdToUse.prepareMethodOverrides();
		}
		// 1、代理bean对象创建--AOP
		try {
			// 调用InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation
			// (如果创建了bean,则还要调用BeanPostProcessor的postProcessAfterInitialization)

			// 调用用于生成AOP的代理对象的BeanPostProcessor,如果bean不为null,则说明产生代理对象了,可以直接返回

			// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
			Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
			if (bean != null) {
				// 如果bean不为null ,则直接返回,这种情况通常为AOP创建了代理对象。后面的doCreateBean不再执行
				return bean;
			}
		}

		// 2、普通bean对象创建
		try {
			// 正常的,非代理bean对象的创建
			// 包括检查是否进行了类加载(没有则进行类加载),bean对象实例创建,bean对象实例的属性赋值,init-method的调用,BeanPostProcessor的调用
			Object beanInstance = doCreateBean(beanName, mbdToUse, args);
			if (logger.isDebugEnabled()) {
				logger.debug("Finished creating instance of bean '" + beanName + "'");
			}
			return beanInstance;
		}
	}

由以上代码分析可知,代理对象的创建就是在resolveBeforeInstantiation方法实现的,它会调用InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation前置处理(这是在实例化Bean对象之前执行的),如果创建的bean不为空,说明产生了代理对象,则还要马上调用BeanPostProcessor的postProcessAfterInitialization后置处理,下面再具体分析。

这里resolveBeforeInstantiation方法,假如返回了目标对象的的代理对象(bean不为空),就会跳过后面普通bean对象创建的过程,否则就继续往下执行第2步,至于普通bean对象的创建流程不是本文的重点,不过多介绍。

【重点】

AbstractAutowireCapableBeanFactory#resolveBeforeInstantiation():

	@Nullable
	protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
		Object bean = null;
		if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
			// Make sure bean class is actually resolved at this point.
			if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
				Class<?> targetType = determineTargetType(beanName, mbd);
				if (targetType != null) {

					// 调用InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation
					// 如果返回不是null,则一般是AOP的代理对象
					bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);

					if (bean != null) {
						// 如果使用postProcessBeforeInstantiation创建了bean对象,则是当前bean对象的代理对象了
						// 这个方法执行完,退出则直接返回该代理bean对象了,
						// 故在这里调用一下其他BeanPostProcessor的postProcessAfterInitialization
						// initialization
						// 调用普通BeanPostProcessor的postProcessAfterInitialization
						bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
					}
				}
			}
			mbd.beforeInstantiationResolved = (bean != null);
		}
		return bean;
	}

首先调用applyBeanPostProcessorsBeforeInstantiation前置处理方法来创建代理对象,如果创建成功,即bean不为空,则继续调用applyBeanPostProcessorsAfterInitialization方法来调用BeanPostProcessor子类的postProcessAfterInitialization方法,因为如果创建了代理对象,该方法返回后就直接return该代理对象bean了,不再往下执行,故在这里就要马上调用BeanPostProcessor的postProcessAfterInitialization执行后置处理。

AOP相关的代理对象的创建,就在AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInstantiation():

	protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
		// 获取所有的Bean后置处理器,然后遍历查看是否为子接口InstantiationAwareBeanPostProcessor,
		// 如果是则调用其postProcessBeforeInstantiation方法
		for (BeanPostProcessor bp : getBeanPostProcessors()) {

			// 查找实现了InstantiationAwareBeanPostProcessor接口的BeanPostProcessor
			// 其中在spring-aop模块定义的AspectJAwareAdvisorAutoProxyCreator就实现了InstantiationAwareBeanPostProcessor接口
			if (bp instanceof InstantiationAwareBeanPostProcessor) {
				InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
				// AbstractAutoProxyCreator 子类的 postProcessBeforeInstantiation()
				Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
				// 如果创建成功则直接返回
				if (result != null) {
					return result;
				}
			}
		}
		return null;
	}

上述源码主要作用:获取所有的BeanPostProcessors(bean后置处理器对象),然后遍历它们是否为InstantiationAwareBeanPostProcessor的子接口实例,如果是则调用其postProcessBeforeInstantiation方法。

看到这里,离真相不远,我们上面开篇已经分析过了,InstantiationAwareBeanPostProcessor接口是BeanPostProcessor接口的子接口,而AspectJAwareAdvisorAutoProxyCreator(AOP代理对象创建器)的抽象父类AbstractAutoProxyCreator实现了InstantiationAwareBeanPostProcessor接口。

postProcessBeforeInstantiation

目标对象实例化之前调用,该方法返回值类型是Object,也就是说可以返回任何类型的值,此时我们可以返回原本目标的代理对象,或者返回任意的其他对象。如果该方法返回值代替了原本该生成的目标对象,那么后续的很多流程都会跳过,只执行postProcessAfterInitialization方法。

所以,当遍历到AspectJAwareAdvisorAutoProxyCreator的对象时,代码执行到这里,就会执行AspectJAwareAdvisorAutoProxyCreator的postProcessBeforeInstantiation方法,具体为在抽象父类AbstractAutoProxyCreator中定义了postProcessBeforeInstantiation方法的实现。为了直观效果,画了个简单的关系链:

AspectJAwareAdvisorAutoProxyCreator ---> AbstractAutoProxyCreator ---> InstantiationAwareBeanPostProcessor --->  BeanPostProcessor

因此,下面进入AbstractAutoProxyCreator#postProcessBeforeInstantiation前置处理方法逻辑:

	// 当bean对象实例化的创建过程中,在创建bean对象实例之前,先调用这个方法,看是否需要创建一个AOP代理对象直接返回
	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		Object cacheKey = getCacheKey(beanClass, beanName);

		// 返回null,则表示不属于AOP,不需要为目标对象创建代理对象
		if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(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.
		TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
		if (targetSource != null) {
			if (StringUtils.hasLength(beanName)) {
				this.targetSourcedBeans.add(beanName);
			}

			// specificInterceptors类型为Advisor[],是当前bean需要的辅助功能列表
			// 因为Advisor集成了pointcut和advice,故可以知道当前bean是否在pointcut拦截范围内,
			// 如果在获取配置对应的advice列表,该列表作为代理对象的interceptor方法拦截器
			// getAdvicesAndAdvisorsForBean由子类实现
			Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);

			// 重点:基于以上辅助功能列表,创建该bean对应的代理对象proxy
			Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
			this.proxyTypes.put(cacheKey, proxy.getClass());
           
			// 只有创建了proxy,才不返回null
			return proxy;
		}

		return null;
	}

通过在spring-aop配置解析时创建的advisors来为该目标bean查找对应的辅助方法advices,其中advisor为集成了pointcut和advice。这些辅助方法advices作为代理对象的方法拦截器列表specificInterceptors,即代理对象拦截目标对象的方法执行,然后在方法执行前后可以执行该方法拦截器列表对应的方法,从而为目标对象的方法添加这些辅助功能。

创建代理对象:createProxy

createProxy的源码如下:代理对象可以基于JDK的动态代理或者基于Cglib来创建,在Spring中默认为基于JDK的动态代理(在SpringBoot2.x中默认是Cglib,具体可看《SpringBoot2.x中为何默认使用Cglib》),如果ProxyConfig配置类的proxy-target-class属性为true或者目标类没有实现接口,则使用Cglib来创建代理对象。

	protected Object createProxy(Class<?> beanClass, @Nullable String beanName,
								 @Nullable Object[] specificInterceptors, TargetSource targetSource) {

		if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
			AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
		}

		// 每个bean对象都new一个单例的ProxyFactory来创建代理对象,因为每个bean需要的辅助方法不一样,
		// 然后将该ProxyFactory对象引用作为构造函数参数创建对应的代理对象
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.copyFrom(this);

		// // 检查是否配置了<aop:config />节点的proxy-target-class属性为true-->cglib
		if (!proxyFactory.isProxyTargetClass()) {
			if (shouldProxyTargetClass(beanClass, beanName)) {
				proxyFactory.setProxyTargetClass(true);
			} else {
				evaluateProxyInterfaces(beanClass, proxyFactory);
			}
		}


		Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);

		// 为该代理工厂添加辅助功能包装器Advisors,结合Advisors来生成代理对象的方法拦截器
		proxyFactory.addAdvisors(advisors);

		// 目标类
		proxyFactory.setTargetSource(targetSource);
		customizeProxyFactory(proxyFactory);

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

		// 为目标类创建代理对象,如果配置了aop:config的proxy-target-class为true,则使用CGLIB
		// 否则如果目标类为接口则使用JDK代理,否则使用CGLIB
		return proxyFactory.getProxy(getProxyClassLoader());
	}

上面源码注释已经写得很清楚,下面进入proxyFactory.getProxy方法,由于ProxyFactory继承ProxyCreatorSupport类,所以进入该方法后可知,真正的实现在ProxyCreatorSupport类中,调用的方法是ProxyCreatorSupport类中的方法,而ProxyFactory只是做了一层封装,

    public Object getProxy(ClassLoader classLoader) {
        // 下面的createAopProxy()方法是ProxyCreatorSupport类中的方法
        return createAopProxy().getProxy(classLoader);
    }

重点来了,下面将浮现jdk和cglib代理的真正面目

createAopProxy()方法返回的是AopProxy接口类型,它有两个实现类,分别是:

  1. CglibAopProxy(通过cglib方式生成代理对象)
  2. JdkDynamicAopProxy(通过JDK动态代理方式生成对象)

AopProxy的作用是用于生成代理对象,稍后将会分析这两种不同的实现方式。

那么,CglibAopProxy或者JdkDynamicAopProxy又是如何生成的呢?进入createAopProxy方法,该方法就是获取AopProxy的地方,由方法可知,这里使用了AopProxyFactory来创建AopProxy,而AopProxyFactory使用的是DefaultAopProxyFactory默认的代理工厂类。

public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {

	@Override
	public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
		// 选择jdk动态代理 或 cglib 代理
		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.");
			}
			// 如果targetClass是接口类,那么使用JDK来生成代理对象,返回JdkDynamicAopProxy类型的对象
			if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
				return new JdkDynamicAopProxy(config);
			}
			// 否则,返回ObjenesisCglibAopProxy类型的对象,它是使用cglib的方式生成代理对象
			return new ObjenesisCglibAopProxy(config);
		} else {
			return new JdkDynamicAopProxy(config);
		}
	}

扩展:Cglib代理,原理是生成目标类的子类(即这个子类对象就是代理对象),它是在内存中构件一个子类对象,从而实现对目标对象的功能拓展。不管有没有实现接口都可以使用Cglib代理, 而不是只有在无接口的情况下才能使用。具体可以查看《【Java必备】Java代理模式(静态代理、JDK/Cglib动态代理)》

到这里,我们已经基本清楚AopProxy对象是如何生成的了,接下来我们介绍CglibAopProxy和JdkDynamicAopProxy又是如何生成代理对象的?

JDK动态代理

基于接口实现,通过实现目标类所包含的接口的方法来实现代理,即返回的代理对象为接口的实现类。由于是基于JDK的动态代理实现,即实现了JDK提供的InvocationHandler接口,故在运行时在invoke方法拦截目标类对应被代理的接口的所有方法的执行:获取当前执行的方法对应的方法拦截器链,然后通过反射执行该方法时,在方法执行前后执行对应的方法拦截器

JdkDynamicAopProxy#getProxy

	@Override
	public Object getProxy(@Nullable ClassLoader classLoader) {
		if (logger.isDebugEnabled()) {
			logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource());
		}
		Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true);
		findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
		return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
	}

JdkDynamicAopProxy#getProxy

// JDK的动态代理是基于接口的,故只能代理接口中定义的方法。
// 该类需要通过代理工厂,具体为继承了AdvisedSupport的代理工厂来创建,而不是直接创建,
// 因为AdvisedSupport提供了AOP的相关配置信息,如Advisors列表等。
final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable {

    // 重写InvocationHandler接口的invoke方法
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		MethodInvocation invocation;
		Object oldProxy = null;
		boolean setProxyContext = false;

		TargetSource targetSource = this.advised.targetSource;
		Object target = null;

		try {
			if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
				// The target does not implement the equals(Object) method itself.
				return equals(args[0]);
			} else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
				// The target does not implement the hashCode() method itself.
				return hashCode();
			} else if (method.getDeclaringClass() == DecoratingProxy.class) {
				// There is only getDecoratedClass() declared -> dispatch to proxy config.
				return AopProxyUtils.ultimateTargetClass(this.advised);
			} else if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
					method.getDeclaringClass().isAssignableFrom(Advised.class)) {
				// Service invocations on ProxyConfig with the proxy config...
				return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
			}

			Object retVal;

			if (this.advised.exposeProxy) {
				// Make invocation available if necessary.
				oldProxy = AopContext.setCurrentProxy(proxy);
				setProxyContext = true;
			}

			// Get as late as possible to minimize the time we "own" the target,
			// in case it comes from a pool.
			target = targetSource.getTarget();
			Class<?> targetClass = (target != null ? target.getClass() : null);

			// 获取该方法对应的方法拦截器列表
			// 实现:通过该方法所在类对应的Advisors,获取该方法的辅助功能Advices列表,即方法拦截器列表。这里的实现为懒加载,
			// 即当方法第一次调用的时候才创建该方法拦截器列表,然后使用一个ConcurrentHashMap缓存起来,之后的方法调用直接使用。

			// 其中advised就是该方法的所在bean对应的ProxyFactory对象引用,通过ProxyFactory来创建AopProxy,即当前类对象实例。
			// Get the interception chain for this method.
			List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

			// Check whether we have any advice. If we don't, we can fallback on direct
			// reflective invocation of the target, and avoid creating a MethodInvocation.
			// 当前执行的方法不包括方法拦截器,即不需要额外的辅助功能,则可以直接执行
			if (chain.isEmpty()) {
				// We can skip creating a MethodInvocation: just invoke the target directly
				// Note that the final invoker must be an InvokerInterceptor so we know it does
				// nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
				Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
				retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
			} else {
				// 如果当前方法包括方法拦截器,即在执行时需要其他额外的辅助功能,则创建ReflectiveMethodInvocation
				// We need to create a method invocation...
				invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
				// Proceed to the joinpoint through the interceptor chain.
				retVal = invocation.proceed();
			}

			// Massage return value if necessary.
			Class<?> returnType = method.getReturnType();
			if (retVal != null && retVal == target &&
					returnType != Object.class && returnType.isInstance(proxy) &&
					!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
				// Special case: it returned "this" and the return type of the method
				// is type-compatible. Note that we can't help if the target sets
				// a reference to itself in another returned object.
				retVal = proxy;
			} else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
				throw new AopInvocationException(
						"Null return value from advice does not match primitive return type for: " + method);
			}
			return retVal;
		} finally {
			if (target != null && !targetSource.isStatic()) {
				// Must have come from TargetSource.
				targetSource.releaseTarget(target);
			}
			if (setProxyContext) {
				// Restore old proxy.
				AopContext.setCurrentProxy(oldProxy);
			}
		}
	}
。。。。

CGLIB代理

创建目标类的子类来实现代理,代理拦截的只能是目标类的publicprotected方法,核心方法为getProxy(),即创建代理对象,在这里织入了辅助功能。

注意:基于类的代理,具体实现为通过创建目标类的子类来实现代理,即代理对象对应的类为目标类的子类。 所以目标类的需要被代理的方法不能为final,因为子类无法重写final的方法;同时被代理的方法需要是public或者protected,不能是static,private或者包可见,即不加可见修饰符。如在事务中,@Transactional注解不能对private,static,final,包可见的方法添加事务功能,只能为public方法。这个代理对象也需要通过代理工厂来创建,具体为继承了AdvisedSupport的代理工厂来创建,而不是直接创建。


class CglibAopProxy implements AopProxy, Serializable {

	// 创建代理对象
	@Override
	public Object getProxy(@Nullable ClassLoader classLoader) {
		if (logger.isDebugEnabled()) {
			logger.debug("Creating CGLIB proxy: target source is " + this.advised.getTargetSource());
		}

		try {
			//获取目标对象
			Class<?> rootClass = this.advised.getTargetClass();
			Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy");

			Class<?> proxySuperClass = rootClass;
			if (ClassUtils.isCglibProxyClass(rootClass)) {
				proxySuperClass = rootClass.getSuperclass();
				Class<?>[] additionalInterfaces = rootClass.getInterfaces();
				for (Class<?> additionalInterface : additionalInterfaces) {
					this.advised.addInterface(additionalInterface);
				}
			}

			// Validate the class, writing log messages as necessary.
			validateClassIfNecessary(proxySuperClass, classLoader);

			// 创建目标类的子类来实现代理,即织入辅助功能
			// 创建并配置Enhancer,它是cglib主要的操作类,用于代理对象的生成
			// Configure CGLIB Enhancer...
			Enhancer enhancer = createEnhancer();
			if (classLoader != null) {
				enhancer.setClassLoader(classLoader);
				if (classLoader instanceof SmartClassLoader &&
						((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
					enhancer.setUseCache(false);
				}
			}
			// 配置enhancer对象,比如 代理接口,父类,回调方法等
			enhancer.setSuperclass(proxySuperClass);
			enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
			enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
			enhancer.setStrategy(new ClassLoaderAwareUndeclaredThrowableStrategy(classLoader));

			Callback[] callbacks = getCallbacks(rootClass);
			Class<?>[] types = new Class<?>[callbacks.length];
			for (int x = 0; x < types.length; x++) {
				types[x] = callbacks[x].getClass();
			}
			// fixedInterceptorMap only populated at this point, after getCallbacks call above
			enhancer.setCallbackFilter(new ProxyCallbackFilter(
					this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
			enhancer.setCallbackTypes(types);

			// 通过enhancer来生成代理对象  
			// Generate the proxy class and create a proxy instance.
			return createProxyClassAndInstance(enhancer, callbacks);
		} 
        。。。。
	}

到此,目标对象的代理对象已经生成,并且已经设置好了拦截器(通知),当代理对象调用目标方法时,就会触发这些拦截器。

参考:

【Spring AOP源码】基于JDK动态代理和Cglib创建代理对象的原理分析

史上最强Tomcat8性能优化

阿里巴巴为什么能抗住90秒100亿?--服务端高并发分布式架构演进之路

B2B电商平台--ChinaPay银联电子支付功能

学会Zookeeper分布式锁,让面试官对你刮目相看

SpringCloud电商秒杀微服务-Redisson分布式锁方案

查看更多好文,进入公众号--撩我--往期精彩

一只 有深度 有灵魂 的公众号0.0【Spring AOP源码】基于JDK动态代理和Cglib创建代理对象的原理分析

【Spring AOP源码】基于JDK动态代理和Cglib创建代理对象的原理分析

 

本文地址:https://blog.csdn.net/a1036645146/article/details/112236026