<aop:aspectj-autoproxy proxy-target-class= "true" expose-proxy= "true"/>
Aopnamespacehandler registered Aspectjautoproxybeandefinitionparser,
Aspectjautoproxybeandefinitionparser also registered the Annotationawareaspectjautoproxycreator.
Annotationawareaspectjautoproxycreator implements the Beanpostprocessor, The class that implements this interface calls the Postprocessafterinitialization method when it gets the bean.
The Wrapifnecessary method is then called.
Wrapifnecessary Method:
1 Getadvicesandadvisorsforbean Get Enhancer
Get the enhancer in two steps, get all the enhancer in the first step, and get the enhancer for the current class in the second part.
1.1 Get all the enhancer
The calling procedure is to delegate the Aspectjadvisorsbuilder to find all the enhancers, and then Aspectjadvisorsbuilder uses the advisorfactory to get the enhancer.
In the Getadvisor method of advisorfactory, different enhancers, before,after, and so on, are created according to different annotations.
Then use Instantiationmodelawarepointcutadvisorimpl to encapsulate them.
1.2 Getting the right enhancer
Get to all the enhancers and find the right enhancer in the canapply using Aoputils.
If the advisor is a pointcutadvisor subclass, it will use the advisor's pointcut to match,
For example, spring's transaction beanfactorytransactionattributesourceadvisor, which uses transactionattributesourcepointcut to see if it uses the
Use this advisor if you can.
2 Createproxy Create an agent
The proxy class is created in the Proxyfactory GetProxy method, where Aopproxyfactory is called to create aopproxy (for example, Jdkdynamicaopproxy or
Objenesiscglibaopproxy), aopproxyfactory the default implementation class is Defaultaopproxyfactory, which gets aopproxy after using Aopproxy to create the proxy.
Familiar with the dynamic agent know that the proxy class will invoke the Invoke method, the Invoke method mainly do the following 2 work.
2.1. Get the Interceptor chain
List<object> chain = This.advised.getInterceptorsAndDynamicInterceptionAdvice (method, Targetclass);
Advised is a previously created proxyfactory that has been commissioned by Advisorchainfactory,advisorchainfactory with only one default implementation
Class, Defaultadvisorchainfactory, which encapsulates advice into methodinterceptor and then encapsulates it into Interceptoranddynamicmethodmatcher.
2.2 Use Reflectivemethodinvocation to encapsulate the interceptor chain and then invoke.
invocation = new Reflectivemethodinvocation (proxy, Target, method, args, Targetclass, chain);
Proceed to the Joinpoint through the interceptor chain.
RetVal = Invocation.proceed ();