This series of articles is my study Spring In the process of summing up , Which involves the relevant source code , May not be very friendly to readers , Please combine with my source code comments Spring Source code analysis GitHub Address To read .

Spring edition :5.1.14.RELEASE

At the beginning of reading Spring AOP Before source code , Need to be right Spring IoC Have some understanding , Check out my 《 screwing Spring And IoC piece - Article reading guidance 》 This series of articles

understand AOP Related terms , You can check 《Spring AOP Frequently asked questions ) 》 This article

For other articles in the series, check out :《 screwing Spring And AOP piece - Article reading guidance 》

Through the previous article 《 First time to know JDK、CGLIB Two kinds of dynamic agents 》 In this article, we are interested in Spring AOP At the bottom JDK Dynamic proxy sum CGLIB Dynamic agents have a certain understanding of , You also know how to simply create proxy objects using two dynamic proxies . I believe the last article can help you understand Spring AOP Have a preliminary understanding , So next we're going to go into Spring AOP Source code learning stage .

At the beginning Spring AOP Before learning the source code , This article will Spring AOP Most of the major ones involved are API To introduce , Let's be right Spring AOP Have a comprehensive understanding of , This is learning Spring AOP Having a clear idea in the process of source code will make it easier to understand , And then in the following article from Spring AOP Of Automatic agent Start learning in depth , Let's get straight to the point .

Spring AOP API The overview

As shown in the figure below :

The picture above lists Spring AOP Most of them API, Next, let's briefly introduce :

  • Joinpoint: Connection point , That's what we need to do

  • Pointcut: Tangent point , Provide ClassFilter Class filters and MethodMatcher The method matcher supports class and method filtering .

  • Advice: Notifier , It's usually MethodInterceptor method interceptors , If not, it will pass AdvisorAdapter The adapter converts to the corresponding MethodInterceptor method interceptors .

  • Advisor:Advice Container interface , And Advice It's a one-to-one relationship ; Its sub interface PointcutAdvisor Equivalent to a kind of Pointcut and Advice The container of , take Pointcut Filter Joinpoint Ability and Advice Integration , That's how it connects the two .

  • Advisor Adapter AdvisorAdapter yes Advisor Adapter for , When you filter out all the things that can be applied to the method Advisor after , You need to get the corresponding Advice;

    • If not MethodInterceptor type , You have to go through AdvisorAdapter The adapter converts to the corresponding MethodInterceptor method interceptors .
  • AOP Proxy object :AOP Proxy object , from JDK Dynamic agents or CGLIB Proxy object created by dynamic proxy ;

    • Choose which dynamic agent is through AopProxyFactory The agent factory is determined by the target class .
  • AOP Agent configuration AdvisedSupport The configuration manager stores the configuration information of the corresponding proxy object , For example, if the conditions are met Advisor object 、TargetSource Target class source ;

    • In the process of creating a proxy object ,AdvisedSupport Plays a very important role .
  • AOP Proxy object creation :AOP The creation of proxy objects is divided into Manual mode and Automatic mode ; Either model is similar to AdvisedSupport Configuration manager ;

    • Manual mode It is through Spring AOP Provided API Create ;
    • Automatic mode And Spring IoC Integration , stay Bean If proxy is needed during the loading process of , Then create the corresponding proxy object ;
  • AdvisorChainFactoryAdvisor Link factory

    • AdvisedSupport The configuration manager stores all of the proxy objects Advisor object , When intercepting a method , Need to pass through AdvisorChainFactory The results show that the method can be applied to Advisor People ;
    • In addition, we need the help of AdvisorAdapter The adapter gets Advisor Corresponding MethodInterceptor method interceptors , Will these MethodInterceptor Orderly form a link and return .
  • IntroductionInfo introducing Interface , Support for proxy object implementation. The target class doesn't really implement additional interfaces ;

    • stay Advisor Sub interface of IntroductionAdvisor I'm going to inherit this IntroductionInfo Interface , adopt @DeclareParents The fields defined by the annotation are resolved to IntroductionAdvisor object .
  • AOP Proxy target object source Target class source , Associate with a proxy object , Used to get the target object of the proxy ;

    • In the proxy object, the final method execution needs to go through TargetSource Get the corresponding target object , Then execute the target method .

Joinpoint

Class diagram