Correct understanding of around advice in spring AOP

Source: Internet
Author: User

In Spring AOP, with before advice and after advice, these two advice are literally easy to understand, but around advice is a bit of a hassle.

At first glance it seems to be a combination of before advice and after advice, which means that pointcut executes once before and after Joinpoint execution. But this understanding is not correct, and if so, it raises the question ofwhy the spring AOP around type is only executed once , and this post is what I happen to see.

So what is the right thing to understand? Let's take a look at how spring official explains around advice:

Around advice runs "Around" a matched method execution. It has the opportunity to does work both before and after the method executes, and to determine when, how, and even if, the Method actually gets to execute at all. Around advice is often used if you need to share state before and after a method execution in a thread-safe manner (Starti Ng and stopping a timer for example).

Presumably, Around advice can make decisions about when, how to execute joinpoint, and even decide whether to actually execute Joinpoint method calls, through an opportunity to do something before and after joinpoint execution. Around advice is usually used in the following situations:

In a multithreaded environment, some data needs to be shared before and after the Joinpoint method call. If you use before advice and after advice you can do the same, but you need to create a field in aspect that stores the shared information, and it's not thread-safe.

Now that we understand the purpose of spring design around advice, let's look at the specific usage.

ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Around;Importorg.aspectj.lang.ProceedingJoinPoint; @Aspect Public classaroundexample {@Around ("Com.xyz.myapp.SystemArchitecture.businessService ()")   PublicObject dobasicprofiling (Proceedingjoinpoint PJP)throwsThrowable {//start stopwatch is equivalent to before adviceObject RetVal=pjp.proceed (); //stop stopwatch is equivalent to after advice    returnRetVal; }}

Now you see it, not in the Joinpoint execution before and after each call pointcut, but in Pointcut Joinpoint to around up.

Excerpt from: http://blog.163.com/chen_guangqi/blog/static/2003111492012101653052508/

Correct understanding of around advice in spring AOP

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.