Your vivid illustrations to explain Spring AOP source (1)

Foreword

In Spring AOP - use presentation (long article Comments) notes the way , the author describes the use of Spring AOP annotation mode. Spring AOP to be our source code analysis a start, paving the way little knowledge points.

Before starting Spring AOP source code, if you have not studied Spring IoC source, it is best to go to study at Spring IoC.

Spring AOP only act on the Spring Bean features illustrates the relationship of Spring AOP and Spring IOC, AOP depends on the IOC container to manage the back of the source code analysis also involves Spring IoC source content.

Now, assuming you've learned Spring IoC related content and related use of Spring AOP, let's get started.

In this paper the author spent a lot of effort, hoping to help you.

Spring AOP.png

We've been talking about Spring AOP source parsing source code so much, what we are really concerned with?

What is the function of Spring AOP? From the use of straightforward to say, it is to generate proxy classes based on our configurations, intercept method specified, the specified advice weaving.

What we should be concerned conclude that:

  • Spring AOP trigger timing is when?
  • Spring AOP Aspect is how we resolve configured to generate Advisors chain?
  • Spring AOP is how to generate proxy classes, advice on how to weave into the proxy class?

In addition, the entire contents of the source code parsing too much for the reader's reading experience and their own schedule. I will interpret to you at the three points, three points above summary.

Source of this article is resolved comments AOP way to use as an example to explain, and in other ways that are different major trigger entry, the core of the process is the same. Hope readers can comprehend by analogy.

First, turn on automatic proxy AOP mystery

We in Spring AOP - use presentation (long article explain) Notes manner described in the @EnableAspectJAutoProxynotes, is to enable the use of Spring AOP annotations. This role is automatic for all advisor ioc container to match the method, internal advisor are all advice, so that their internal advice to execution interception process (Note: advisor can then be seen as pointcut + advise of a combination of objects). This quote is annotated English translation automatically open proxy.

So what's inside the mystery is it?

We went inside to see advanced to this comment,

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
    boolean proxyTargetClass() default false;
    boolean exposeProxy() default false;
}
复制代码

@Import(AspectJAutoProxyRegistrar.class)Use @Importannotation to AspectJAutoProxyRegistrarinject them into the IoC container.

This comment is not familiar with can go to find out @Import Annotation in Spring Framework

We look at this AspectJAutoProxyRegistrar,

AspectJAutoProxyRegistrar.png

Note that this class implements the ImportBeanDefinitionRegistrarinterface.

This interface is a Spring very strong expansion interface , its role is to:

Register additional bean definitions when processing @Configuration classes. Useful when operating at the bean definition level (as opposed to @Bean method/instance level) is desired or necessary.

That is, it needs to and @Configurationused in conjunction with , the @Configurationpreviously registered Bean, can be made ImportBeanDefinitionRegistrarto deal with the interface, this interface provides a method as follows:

void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry)
复制代码

This method can get @Importin this class Annotation Metadata, and this time the BeanDefinitionRegistryobject, BeanDefinitionRegistryyou can get BeanDefinition all currently registered, you can customize the logic to dynamically register some of you feel necessary BeanDefinition.

PS: Many open-source framework and Spring integration when they are extended this interface, such as Apollo's ApolloConfigRegistrar, mybatis of MapperScannerRegistrar etc.

Further reading www.logicbig.com/tutorials/s...

In the AspectJAutoProxyRegistrarmiddle, in fact, to AspectJAnnotationAutoProxyCreatorthe BeanDefinitionregistration to which IoC container.

The following is a AopConfigUtilslogic of the code segment register of the execution.

image.png


After a first-come, dividing line, complete understanding of the above processes, we continue to think.

Why is AspectJAnnotationAutoProxyCreatorinjected into the Spring IoC container, the agent will automatically open it?

Let's find the trigger point.

Second, the agent automatically triggering occasion

First, we look at AspectJAnnotationAutoProxyCreatorinheritance structure.

AnnatationAwareAspectJAutoProxyCreator.png

There are not found, AspectJAnnotationAutoProxyCreatoris actually a BeanPostProcessor!

You should be extremely sensitive to this class after learning through the Spring IoC.

public interface BeanPostProcessor {
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
复制代码

Let's look back at the realization of ideas proxy mode of thinking: (Interface) + + implementation class real proxy class .

Is not the first real implementation class have to be able to generate proxy classes? !

In the Spring IoC - Dependency injection source code parsing , we introduced the process of Spring Bean created in executing the Step1 create instances of objects createBeanInstance()and properties Step2 assembly populateBean()After that we considered getting a real implementation class .

In Step3 initializeBean()in, IoC container will handle a variety of events after the callback Bean initialization, and then return to a "possible processed" bean object.

Which include the BeanPostProcessorof postProcessBeforeInitializationcallbacks and postProcessAfterInitializationcallbacks.

And AspectJAnnotationAutoProxyCreatorit is precisely a BeanPostProcessor(forgive I repeated once), it is very easy to think, Spring AOP is in this step, proxy enhancements !

Third, the proxy class generation process Study

So then, we take a look inside this mystery.

image.png

You can see the actual callback postProcessBeforeInitializationand postProcessAfterInitializationthis is in two ways AbstractAdvisorAutoProxyCreatorin the override.

Source Location:AbstractAdvisorAutoProxyCreator

AbstractAdvisorAutoProxyCreator

JavaDoc clearly indicated the postProcessAfterInitializationwill to perform operations to create a proxy class configuration with interceptors to create a proxy class, and told us to see getAdvicesAndAdvisorsForBean, it seems that this will be a key method, where we first do not worry, read on wrapIfNecessarymethod.

Source Location:AbstractAutoProxyCreator#wrapIfNecessary(..)

wrapIfNecessary

The core of this approach there are two points that I have used in the above figure **** TODO-1 ****and **** TODO-2 ****identify out.

TODO-1It is to get the current Spring Bean adaptation of Advisors .

TODO-2It is to create a proxy class .


Our next chapter is to explain in detail both TODOthe content. We will next time.

If this article helpful to you, hoping to spot a praise, this is the biggest driving force for me.

Guess you like

Origin juejin.im/post/5dc5560251882521f2623edc