Spring 5.x Source trip forty-two AOP of @EnableAspectJAutoProxy

What did EnableAspectJAutoProxy comment

This annotation is the key to open the AOP proxy, he is not really the key attributes inside the open, proxyTargetClassbut can either decide to use CGLIBit. The real decision can proxy is AspectJAutoProxyRegistrar.class.
Here Insert Picture Description

AspectJAutoProxyRegistrar Acting Registrar

He is a ImportBeanDefinitionRegistrarexpansion, we have talked about earlier, you can sign up for a beandefinition.

class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar {

	/**
	 * Register, escalate, and configure the AspectJ auto proxy creator based on the value
	 * of the @{@link EnableAspectJAutoProxy#proxyTargetClass()} attribute on the importing
	 * {@code @Configuration} class.
	 */
	@Override
	public void registerBeanDefinitions(
			AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

		AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);

		AnnotationAttributes enableAspectJAutoProxy =
				AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
		if (enableAspectJAutoProxy != null) {
			if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
				AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
			}
			if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
				AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
			}
		}
	}

}

What time of registration into it, in fact, in front also say that in class configuration parsing, after verification, were beanregistered when loaded into a defined, we take a look back just great.

Configuration classes

My configuration class:

@Configuration
@EnableAspectJAutoProxy
public class MyConfig {

}

Resolution Configuration class

ConfigurationClassPostProcessorThe processConfigBeanDefinitionsmethod.
Here Insert Picture Description
ConfigurationClassParserThe doProcessConfigurationClassmiddle:
Here Insert Picture Description
First getImportswill be comments, and the comments of the parent Importvalue of the notes come fully loaded:
Here Insert Picture Description
Here Insert Picture Description
So put AspectJAutoProxyRegistrarto find out.
Here Insert Picture Description
Then processImportsfor the ImportBeanDefinitionRegistrartype of treatment.
Here Insert Picture Description
Then instantiated, then placed in the ConfigurationClassin importBeanDefinitionRegistrarsthe, behind the ready to load beandefined by time.

Load bean definitions

ConfigurationClassBeanDefinitionReaderThe loadBeanDefinitionsForConfigurationClasslast line:
Here Insert Picture Description
is to call ImportBeanDefinitionRegistrarthe registerBeanDefinitionsmethod:
Here Insert Picture Description

registerBeanDefinitions

In fact, we created AnnotationAwareAspectJAutoProxyCreatora beandefinition, and then set the property.

@Override
	public void registerBeanDefinitions(
			AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

		AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);

		AnnotationAttributes enableAspectJAutoProxy =
				AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
		if (enableAspectJAutoProxy != null) {
			if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
				AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
			}
			if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
				AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
			}
		}
	}

It is to create an internal bean definitions and registration:
Here Insert Picture Description
Here Insert Picture Description
next time tell AnnotationAwareAspectJAutoProxyCreatorwhat it is.

Well, here today, we hope to help study and understand, do not spray the Great God see, understand only their own learning, limited capacity, please excuse.

Published 235 original articles · won praise 74 · views 30000 +

Guess you like

Origin blog.csdn.net/wangwei19871103/article/details/105184755