Step 1 Initialization Parser Aspectjautoproxybeandefinitionparser step 2 Call Aspectjautoproxybeandefinitionparser Parse function registration Annotationawareaspectjautoproxycreator class Proxy Builder step when parsing a configuration file 3 because Annotationawareaspectjautoproxycreator implements the Beanpostprocessor interface, the post-processing method is invoked in the process of loading the Bean Abstractautoproxycreator class Get an enhanced method to create a proxy
Step 1: Initialize the parser aspectjautoproxybeandefinitionparser
Aspectjautoproxybeandefinitionparser: When a aspectj-autoproxy annotation is encountered, the parser is used to parse it:
The <!--declaration automatically creates a proxy for the bean in the spring container that configures the @aspectj slice, and weaves the slice. -->
Step 2: Call the Aspectjautoproxybeandefinitionparser parse function to register the Annotationawareaspectjautoproxycreator class (proxy builder, When parsing a configuration file)You need to take into account that custom agent generators are inconsistent at the time and need to decide which custom proxy builder to use based on priority defaultadvisorautoproxycreator and beannameautoproxycreator are commonly used, Among them, defaultadvisorautoproxycreator more violent, will be all the sections of the container into all the target objects, and the plane can only be the form of consultants. The beannameautoproxycreator is friendlier, allowing us to selectively weave the objects and sections of the object, and the cut can be a consultant or notice form.
Step 3: Because Annotationawareaspectjautoproxycreator implements the Beanpostprocessor interface, Will invoke the post-processing method (in the Abstractautoproxycreator Class) during the loading of the bean
1. Get Enhancement methods
First step: Get all the intensifier
(1) Get all the beanname, including the parent class (recursive lookup)
(2) Traverse all Beanname to find the class declared as AspectJ annotation
(3) If the slice class is a singleton instantiation model of ASPECTJ, then the slice has only one instance, then the Beanfactoryaspectinstancefactory factory is created. Otherwise create the Prototypeaspectinstancefactory factory (Prototypeaspectinstancefactory inherited the Beanfactoryaspectinstancefactory factory)
(4) For each ASPECTJ annotation class to obtain Intensifier Advisor (Advisor) List Step 1: For each method in the class get the Enhancer Step 2: Add Synchronous instantiation intensifier (P185) step 3: Get the declareparents annotation (encapsulated using declareparentsadvisor)
Get the Enhancer Advisor list Procedure (get the intensifier's process for each method)
(1) to aspectclass each method to obtain the tangent information on the method (i.e. Pointcut, find Before.class, Around.class, After.class, Afterreturning.class, Afterthrowing.class, Pointcut.class), and encapsulated in the Aspectjexpressionpointcut class
(2) to generate enhancements based on the information found for each tangent point, all enhancements are uniformly encapsulated by the implementation class Instantiationmodelawarepointcutadvisorimpl of the advisor.
(3) A method of calling This.aspectjadvisorfactory's Getadvice in the encapsulated class Instantiationmodelawarepointcutadvisorimpl, which encapsulates different enhancements based on different annotation types Atbefore annotations (@Before)-> Create aspectjmethodbeforeadvice:methodbeforeadviceinterceptor atafter annotations (@After)-> Create Aspectjafteradvice: Because Aspectjafteradvice implements interceptors, call atafterreturning annotations directly (@AfterReturning)--> Create Aspectjafterreturningadvice atafterthrowing annotations (@AfterThrowing)-> Create Aspectjafterthrowingadvice Ataround annotation (@Around)-> Create Aspectjaroundadvice
Step Two: Find a matching enhancer
Checks to see if the target class conforms to the Pointcut expression 2. To create an agent
Step One: Create Proxyfactory to create and process proxy classes
Step Two: If you set Proxytarclass to False, add the proxy interface
Step Three: Package the Interceptor into a intensifier
Step Fourth: Create a proxy
A. Conditions
Use the JDK dynamic proxy if the Proxytargetclass property is true or if a proxy interface exists with the Cglib proxy
Public aopproxy createaopproxy (advisedsupport config) throws aopconfigexception {
if (config.isoptimize () | | Config.isproxytargetclass () | | Hasnousersuppliedproxyinterfaces (config)) {
class<?> targetclass = Config.gettargetclass ();
if (Targetclass.isinterface () | | Proxy.isproxyclass (Targetclass)) {return
new Jdkdynamicaopproxy (config);
}
return new Objenesiscglibaopproxy (config);
else {return
new Jdkdynamicaopproxy (config);
}
}
the Invoke method in the B.jdkdynamicaopproxy class (Implementing the Invocationhandler Interface)
Create an interceptor chain and execute one at a time (such as the intensifier created earlier)
C.cglibaopproxy class
Gets all the enhancements and creates the proxy when the parse operation, such as the enhancer, is the spring parsing configuration file.
Invoke the proxy invoke method when calling the Bean method
AOP has three kinds of methods for cutting slices: one is compile-time weaving, which requires the use of special Java compilers, ASPECTJ is a representative of them, the second is the class loading period weaving, and this requires the use of special class loaders, ASPECTJ and Aspectwerkz are the representatives; The third is for dynamic agents to weave, and in the runtime to add enhanced generation subclasses to the target class, Spring AOP uses dynamic proxies to weave the slices.