[Spring] AOP (Article 2)

Source: Internet
Author: User

In the previous article, the concept and xml configuration of a plane section were briefly introduced by taking the UFC boxers as an example.

This description refers to the cut point reference, surround notification, and notification obtaining parameters.


Last time I said that Bruce Buffer was responsible for introduce.

But in fact, not only Bruce Buffer is the one who needs to work when the boxers enter. We also have a lot of staff who need to do different jobs when they enter.
Return to our configuration file and think: Do we need to write a pointcut expression for every notification? The answer must be no.

<Aop: config> <! -- Define a pointcut separately --> <aop: pointcut expression = "execution (* pac. bean. fighter. entrance (..)) "id =" fighterEntrance "/> <aop: aspect ref =" bruceBuffer "> <! -- Reference this pointcut if needed --> <aop: before pointcut-ref = "fig?entrance" method = "introduce"/> </aop: aspect> </aop: config>

It seems much more comfortable now.


Now let's take a look at the notifications.
For example ~ The audience watched the competition and looked for their seats before they started. After the competition, they cheered with excitement. They couldn't help themselves when they saw the boxers being kicked to the opening department ~ .
These are all the moves we made when watching the game. Let's turn him into a heartbeat notice.
First, let's look at the movements of the audience:

public void watchReaction(ProceedingJoinPoint joinPoint){        try {            System.out.println("taking seats..");            joinPoint.proceed();            System.out.println("UFC!UFC!UFC!");        } catch (Throwable e) {            System.out.println("Wooo..");        }    }

Then configure it in xml:

<aop:pointcut expression="execution(* pac.bean.Fighter.fight(..))" id="fighterFight" /><aop:around method="watchReaction" pointcut-ref="fighterFight" />

It seems nothing special. However, there is a ProceedingJoinPoint parameter in the wrap-around method. As the name suggests, it is easy to understand that proceed () allows the notified method to be executed. Of course, this method can be executed multiple times.

Finally, Let's explain <parameters for obtaining the notification method by using the notification method>.
Now I want to change the boxers to the following:

package pac.bean;public class Fighter {    private String name;    private String nickName;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getNickName() {        return nickName;    }    public void setNickName(String nickName) {        this.nickName = nickName;    }    public void entrance(String nameCard) {        System.out.println("Yo!" + nameCard + " comin!!");    }}

The key is that I have added the nameCard parameter to the entrance method. I just want to name myself like this, and I hope Bruce Buffer will work with me when introducing it.

So Bruce Buffer's introduce () is also changed to the following:

public void introduce(String name){        System.out.println("Introducin first,Here comes the ..."+name);    }

Finally, let's take a look at xml:

<aop:pointcut expression="execution(* pac.bean.Fighter.entrance(..)) and args(asdfasdf)" id="fighterEntrance" />        <aop:aspect ref="bruceBuffer">            <aop:before pointcut-ref="fighterEntrance"            method="introduce" arg-names="asdfasdf"/>

We can see that the args in the cut point definition corresponds to the arg-names in the notification, and it is written by me to demonstrate that this has nothing to do with the parameter name in method.

Okay, so Bruce Buffer will introduce me based on the value of my nameCard.


This article is from "It's SWFUpload !!" Blog, please be sure to keep this source http://runtime.blog.51cto.com/7711135/1294833

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.