The ExposeInvocationInterceptor interceptor function in the chain call of Spring AOP source code exploration

Article directory

    • ExposeInvocationInterceptor
      • sample code
      • Source code analysis
        • extendAdvisors
        • makeAdvisorChainAspectJCapableIfNecessary
      • Add the call chain of the extended interceptor
      • Function example
    • Summarize
  • Related Learning Paths
    • JAVA Senior Architect Growth Route -> Open Source Framework Interpretation -> Spring Framework Source Code Interpretation

ExposeInvocationInterceptor

From the English name, as the name implies, it exposes the interceptor of the caller.
It is an interceptor that exposes an invoker.

  • 1. So what invoker does it expose?
  • 2. How is it exposed, and by what means?
  • 3. Who is exposed to?
  • 4. When does it play an interception role?

With the above questions, analyze the spring Aop source code through the sample code

Example code

Sample code for Spring AOP source code exploration

Source code analysis

extend Advisors

When the Bean is initialized and the post-processing method postProcessAfterInitialization of AbstractAutoProcxyCreator is called, the adivisors are extended and the ExposeInvocationInterceptor interceptor is added

makeAdvisorChainAspectJCapableIfNecessary

Where the ExposeInvocationInterceptor interceptor is actually added
Author’s note in English:

/**
* Add special advisors if necessary to work with a proxy chain that contains AspectJ advisors.
* This will expose the current Spring AOP invocation (necessary for some AspectJ pointcut matching)
* and make available the current AspectJ JoinPoint. The call will have no effect if there are no
* AspectJ advisors in the advisor chain.
* @param advisors Advisors available
* @return {@code true} if any special {@link Advisor Advisors} were added, otherwise {@code false}.
*/

Add the call chain of the extended interceptor

Method stack diagram

full method stack

makeAdvisorChainAspectJCapableIfNecessary:44, AspectJProxyUtils (org.springframework.aop.aspectj)
extendAdvisors:97, AspectJAwareAdvisorAutoProxyCreator (org.springframework.aop.aspectj.autoproxy)
findEligibleAdvisors:90, AbstractAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy)
getAdvicesAndAdvisorsForBean:70, AbstractAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy)
wrapIfNecessary: 346, AbstractAutoProxyCreator (org.springframework.aop.framework.autoproxy)
postProcessAfterInitialization: 298, AbstractAutoProxyCreator (org.springframework.aop.framework.autoproxy)
applyBeanPostProcessorsAfterInitialization:421, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
initializeBean:1635, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
doCreateBean:553, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:481, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
getObject:312, AbstractBeanFactory$1 (org.springframework.beans.factory.support)
getSingleton:230, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:308, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:197, AbstractBeanFactory (org.springframework.beans.factory.support)
preInstantiateSingletons:761, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:867, AbstractApplicationContext (org.springframework.context.support)
refresh:543, AbstractApplicationContext (org.springframework.context.support)
<init>:84, AnnotationConfigApplicationContext (org.springframework.context.annotation)
main:11, SilasMainClass (org.silas)

Example of function

Works when the proxy object calls a method.
Interpretation of method stack and chain call of Spring AOP source code exploration

The call chain first calls the invoke method of this ExposeInvocationInterceptor interceptor, and sets MethodInvocation mi to
Inside ThreadLocal invocation

Take the invoke of the post notification (@After) interceptor as an example
org.springframework.aop.aspectj.AspectJAfterAdvice#invoke

When calling the notification method invokeAdviceMethod, a parameter is passed, and the parameter is obtained by calling the getJoinPointMatch() method.

Look at the source code of the getJoinPointMatch() method

ExposeInvocationInterceptor.currentInvocation() method
Removed from ThreadLocal invocation
mi previously set through the ExposeInvocationInterceptor instance object

Summary

Then from the above AspectJAfterAdvice#invoke method example, we can know that ExposeInvocationInterceptor is used to pass MethodInvocation. In any subsequent call chain link, as long as the current MethodInvocation is needed, it can be obtained through the ExposeInvocationInterceptor.currentInvocation() static method.

Related learning paths

JAVA Senior Architect’s Growth Route->Open Source Framework Interpretation->Spring Framework Source Code Interpretation