Spring AOP process go through

Source: Internet
Author: User

<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 ();


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.