preface
Previous articles:
-
Spring IoC – Design of Spring IoC
-
Spring IoC – IoC container initialization source code parsing
-
Spring IoC – Dependency injection source code parsing
-
Spring AOP – An introduction to the Use of annotations (long explanation)
-
Spring AOP source code (1)
In the previous chapter (1), which gave you a lively introduction to Spring AOP source code, the author introduced the mystery of turning on AOP automatic proxies and the timing of automatic proxies.
In this chapter, the authors show you how Spring AOP parses our configured Aspect to generate the chain of Advisors.
Without further ado, let’s get straight to the point.
Gets the Advisors chain of the corresponding Bean adaptation
It is divided into two steps to obtain the Advisors chain of the corresponding Bean adaptation.
- As a candidate, get all the advisors of the container, that is, parse the advice methods in all the Aspect classes in the Spring container and wrap them as Advisors;
- The chain of Advisors suitable for the current Bean is screened from the candidate Advisors.
If the reader reads incoherently, we re – paste the last article in our explanation of a section of source code, which continues to tell about.
AbstractAutoProxyCreator#wrapIfNecessary(..)
Source location: AspectJAwareAdvisorAutoProxyCreator# shouldSkip (..)
Source location: AbstractAdvisorAutoProxyCreator# getAdvicesAndAdvisorsForBean (..) , AbstractAdvisorAutoProxyCreator# findEligibleAdvisors (..)
As you can see, both methods call the findCandidateAdvisors() method to get the candidate Advisors. Let’s go inside and see what’s going on.
1. Acquire candidate Advisors
Can be seen from the Debug out thread stack, AnnotationAwareAspectJAutoProxyCreator via BeanFactoryAspectJAdvisorsBuilder objects, to obtain Advisor chain.
Let’s go down a little bit. Source location: BeanFactoryAspectJAdvisorsBuilder# buildAspectJAdvisors
The content is longer, please follow the notes to see it patiently.
Under this method but wait to tell advisorFactory. GetAdvisors (..) In addition, it is important to note that in order to avoid fetching all beanName every time, parsing judgment, the introduction of caching mechanism; Also, the Aspect class is judged based on whether the Spring Bean is decorated by the @aspect annotation.
We next see, true to get our Advisor, the method of enclosing advisorFactory. GetAdvisors (factory) method is as follows:
Source location: ReflectiveAspectJAdvisorFactory# getAdvisors (..)
Parse advice methods into Advisor objects,
Source location: ReflectiveAspectJAdvisorFactory# getAdvisor (..)
Generate the advisor
It is also worth mentioning how advisors are generated.
The advice method we declare in comment mode looks like this. (Check out this article if you’re not familiar with the paradigm.)
@Aspect
@Component
public class PointCutConfig {
/ /... omit
/ / service layer
@Pointcut("within(ric.study.demo.aop.svc.. *) ")
public void inSvcLayer(a) {}
/ /... omit
}
@Aspect
@Component
public class GlobalAopAdvice {
@Before("ric.study.demo.aop.PointCutConfig.inSvcLayer()")
public void logBeforeSvc(JoinPoint joinPoint) {
System.out.println("Print logs at the Service layer");
System.out.println("Method signature of intercepting Service method:"+ joinPoint.getSignature()); }}Copy the code
So after it’s generated, it looks like this,
Object type is InstantiationModelAwarePointcutAdvisorImpl advice, we’ll look at generated advisor called when the constructor of this class,
It includes an important method called instantiateAdvice, which is to create Advice and this is what I want to focus on, how to instantiateAdvice.
Source location: InstantiationModelAwarePointcutAdvisorImpl# instantiateAdvice (..)
Source location: ReflectiveAspectJAdvisorFactory# getAdvice (..)
When you parse the Advisor for the first time
I was also confused at first about the timing of the first parsing Advisor. So let me explain it here.
This is a screenshot of the first trigger posted earlier.
AnnotationAwareAspectJAutoProxyCreator inherited AbstractAutoProxyCreator implements InstantiationAwareBeanPostProcessor interface:
Will be in the generated target before class object, call postProcessBeforeInstantiation (..) Specific code, can go to the AbstractAutowireCapableBeanFactory# createBean (..) Methods. Our side directly see postProcessBeforeInstantiation (..) Implementation in Abstracta toProxyCreator.
2. Filter out Advisors that fit the current class
Here is a split line, and at this point, findCandidateAdvisors() is parsed.
But we just get all the candidate advisors by doing this. Remember the title of our section?
[Obtain the Advisors chain of the corresponding Bean adaptation]
The next step is to filter out advisors that match the current target class.
FindAdvisorsThatCanApply (..)
Search the given candidate Advisors to find all Advisors that can apply to the specified bean.
Find the chain of Advisors that can operate on the current bean from the given candidate Advisors
Debug phase, filter the candidate advisors before and the available advisors after filtering,
Source location: AbstractAdvisorAutoProxyCreator# findAdvisorsThatCanApply (..)
AopUtils#findAdvisorsThatCanApply(..)
Let’s move on to the key method of filtering ‘AopUtils#canApply(..) `
Mainly completed by ClassFilter and MethodMatcher screening work, such as AspectJExpressionPointcut realized ClassFilter and MethodMatcher interface, finally by the AspectJ expression parsing, This is a complicated place, and it’s not a core point.
Another dividing line.
At this point, we are done with the Advisor filtering process.
After sorting, we get the chain of Advisors used by the target class.
summary
To this point, we can review, we are finally toDO-1 [Spring AOP how to obtain the corresponding Bean adaptation of the Advisors chain] introduction is completed, summarize the core logic is:
- Gets all Aspect classes in the current IoC container
- Create a Spring Advisor for the advice method of each Aspect class, which can be subdivided into
- Iterate through all advice methods
- Parsing method annotations and pointcuts
- Instantiate the Advisor object
- Candidate Advisors are obtained and cached for direct retrieval next time
- The Advisor matching the target class is screened from the candidate Advisors
- Gets the pointcut to the Advisor, pointcut
- Gets all public methods of the current Target class
- The traversal method matches the current method with the pointcut’s methodMatcher, and only one match equals the current Advisor adaptation
- Sort the filtered chain of Advisors
- The end of the
In the next section, we will cover the proxy class creation process, and we will see you next time.